立即声明:不将此问题视为实际应用。一切都是为了自学和学习而做的 spin.js
其次,我知道这种关系。我不想用,因为世界比简单的关系更广阔。例如,您要向现在在线的用户或所有参与撰写文章的用户显示文章的页面。
想象一个普通的应用程序,它显示您可以创建、显示、编辑和删除的帖子列表。所有在spine.js 上,一切正常。基于spine.rails3 应用程序。然后我添加了带有字段 post_id 和 body 的 Comment 模型。并在 veiw Show 上添加按钮 Comments。我想要的只是通过按“评论”按钮来加载这篇文章的评论。我的comment.js.coffee:
class App.Comment extends Spine.Model
@configure 'Comment', 'post_id', 'body', 'created_at', 'updated_at'
@extend Spine.Model.Ajax
@fetch_comments: (id) =>
params =
data: {post_id: id}
url: '/comments/fetch'
processData: true
@ajax().fetch(params)
true
在 Rails 控制器中:
def fetch
@comments = Comment.where("post_id = ?", params[:post_id])
respond_with @comments
end
在 chrome 控制台中,一切正常:
App.Comment.fetch_comments(11)
App.Comment.all()
并从服务器接收所有 post_id = 11 的评论。
我的问题是我不知道如何从控制器帖子中做同样的事情:-(在我的posts.js.coffee中:
class Show extends Spine.Controller
events:
......
'click [data-type=show]': 'comments'
comments: ->
@log(@item.id)
new App.Comments(@item.id)
Аnd 这里的问题....如何实现控制器 App.Comments,所以它是从服务器加载需要评论?现在我的控制器看起来像:
Comment = App.Comment
class Index extends Spine.Controller
constructor: (id)->
super
**App.Comment.fetch_comments(id)**
class App.Comments extends Spine.Stack
className: 'comments stack'
controllers:
index: Index
routes:
# '/comments/new': 'new'
'/comments/': 'index'
className: 'stack comments'
并且在选定的行上不起作用。我不知道该怎么办...
主要问题是如何从具有特定参数的另一个控制器中调用一个控制器,并将结果显示在主控制器中?
也许这是菜鸟问题,但我希望对您有所帮助。提前谢谢了。