バックボーン リレーショナルは私には面倒すぎて、デバッグできないようです。このアセットの使用を避けようとしています。
Backbone アプリに 2 つのコレクションがあります。
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) を持つ 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 は悪臭を放ちます。