0

バックボーン リレーショナルは私には面倒すぎて、デバッグできないようです。このアセットの使用を避けようとしています。

Backbone アプリに 2 つのコレクションがあります。

Voice.Collections.PostsVoice.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) を持つ URL に従ってコメント コレクションをフィルター処理するメソッドを持たせたいので、基本的に "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 は悪臭を放ちます。

4

1 に答える 1

1

私の CoffeeScript は少しさびているので、正確に何を思い出せません:

@comCollection = @comCollection.where ->
                  @model.get('post_id') = id

通常の Javascript のように変換します。ただし、正しく使用すれば絶対に機能するはずなので、おそらくより単純な構文を試した場合:

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

あなたはより良い成功を収めることができますか?そうでない場合は、デバッガーに立ち寄って、@comCollectionその呼び出しを行った後に実際に何が含まれているかを確認することをお勧めします。

于 2013-01-03T20:52:25.930 に答える