追加のパラメーターを持つURLを持つモデル(およびそれに対応するコレクション)があります。たとえば、投稿のURLは次のcollection
ようになります。
/rest-api/posts/2/comments/
特定のコメントの場合、URLは次のようになります。
/rest-api/posts/2/comments/3/
コメントコレクションとモデルを定義してインスタンス化するための最良のパターンは何ですか?
私は現在これを行っています:
var Comment = Backbone.Model.extend({
url: function () {
var base = '/rest-api/posts/' + this.get('post_id') + '/comments/';
if (this.isNew()) { return base; }
return base + this.id + '/';
},
// ...
});
var CommentsCollection = Backbone.Collection.extend({
model: Comment,
url: function () {
return '/rest-api/posts/' + this.post_id + '/comments/';
},
initialize: function (options) {
this.post_id = options.post_id;
},
// ...
});
そして、次のようにコレクションをインスタンス化します。
CommentsList = new CommentsCollection({ post_id: current_post_id });
accepted
これはBackbone.jsでこれを行うためのパターンですか?私はこの新しいフレームワークを紹介しています。これを読みやすく保守しやすい方法でコーディングしたいと思います。