骨干关系对我来说太乱了,我似乎无法调试它。我试图避免使用这个资产。
我的Backbone应用程序中有两个集合。
Voice.Collections.Posts
和Voice.Collections.Comments
这是我的路由器:
class Voice.Routers.Posts extends Backbone.Router
routes:
'': 'index'
initialize: ->
@collection = new Voice.Collections.Posts()
@collection.reset($('#container').data('posts').reverse())
index: ->
view = new Voice.Views.PostsIndex(collection: @collection)
$('#container').html(view.render().el)
我希望我的路由器有一个方法可以根据带有帖子ID的网址过滤我的评论集合(作为我的评论 - 发布关系密钥,post_id)所以基本上“posts / 12”(posts /:id)会调用功能showComments:(id) - >这将获取id并初始化一个注释集合,其中只包含'post_id'等于12(“id”)的注释。
我可以从路由器中对集合进行排序吗? 这样的事情? (这不起作用)
class Voice.Routers.Posts extends Backbone.Router
routes:
'': 'index'
'post/:id/': 'showComments'
initialize: ->
@collection = new Voice.Collections.Posts()
@collection.reset($('#container').data('posts').reverse())
index: ->
view = new Voice.Views.PostsIndex(collection: @collection)
$('#container').html(view.render().el)
showComments: (id) ->
@comCollection = new Voice.Views.Collections.Comments()
@comCollection = @comCollection.where ->
@model.get('post_id') = id
comview = new Voice.Views.CommentsIndex(collection: @comCollection)
$('#comContainer').html(comview.render().el)
但这不起作用,因为@comCollection需要初始化。我只是不确定我该怎么做。我也会满足于从另一个视图事件触发器呈现为视图的评论集合。感谢帮助。
编辑:
我是否必须使用Backbone.navigate? Backbone.navigate会产生难闻的气味。
答案 0 :(得分:1)
我的CoffeeScript有点生疏,所以我不记得到底是什么:
@comCollection = @comCollection.where ->
@model.get('post_id') = id
翻译为普通的Javascript。但是,如果使用得当,它绝对应该可以工作,所以如果你尝试了更简单的语法,那么可能是这样的:
this.comCollection = this.comCollection.where({post_id: id});
你可能会有更好的成功吗?如果没有,您可能需要调入调试器并在拨打电话后检查其中@comCollection
实际存在的内容。