Backbone.js 如何避免使用Backbone relational,这是一个简单的博客主干应用程序,带有与帖子相关的评论

Backbone.js 如何避免使用Backbone relational,这是一个简单的博客主干应用程序,带有与帖子相关的评论,backbone.js,coffeescript,underscore.js,backbone-relational,backbone.js-collections,Backbone.js,Coffeescript,Underscore.js,Backbone Relational,Backbone.js Collections,这对我来说太乱了,我似乎无法调试它。我试图避免使用此资产 我的主干应用程序中有两个集合 Voice.Collections.Posts和Voice.Collections.Comments 这是我的路由器: class Voice.Routers.Posts extends Backbone.Router routes: '': 'index' initialize: -> @collectio

这对我来说太乱了,我似乎无法调试它。我试图避免使用此资产

我的主干应用程序中有两个集合

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)
我希望我的路由器有一个方法,根据带有post id(作为我的评论-post关系键,post_id)的url过滤我的评论集合,因此基本上“posts/12”(posts/:id)将调用函数showComments:(id)->它将获取id并初始化只包含“post_id”等于12(“id”)的注释的注释集合

我可以从我的路由器中分类收集吗? 像这样的?(这不起作用)

但这不起作用,因为@comCollection需要初始化。我只是不知道该怎么做。我还同意将评论集作为另一个视图事件触发器的视图呈现。非常感谢您的帮助

编辑:


我需要使用主干网吗?Backbone.navigate会产生一股难闻的气味。

我的咖啡脚本有点生锈,所以我记不清到底是什么:

@comCollection = @comCollection.where ->
                  @model.get('post_id') = id
翻译为普通Javascript。但是,如果使用正确,它绝对会起作用,因此,如果您尝试使用更简单的语法:

this.comCollection = this.comCollection.where({post_id: id});
你可能会有更好的成功?如果没有,您可能需要访问调试器,并在调用后检查
@comCollection
中实际包含的内容

this.comCollection = this.comCollection.where({post_id: id});