特定のコレクションを取得できるシンプルなアプリを実装しようとしていobject_id
ます。
サーバーからのGET response
は次のようになります。
[
{object_id: 1, text: "msg1"},
{object_id: 1, text: "msg2"},
{object_id: 1, text: "msg3"},
.......
]
私の目標は
、ユーザーが を選択したときにコレクションをレンダリングすることobject_id
です。
私のコードの出発点は次のとおりです。
this.options = {object_id: 1};
myView = new NeView(_.extend( {el:this.$("#myView")} , this.options));
私の質問は次のとおりです:
* 最善の方法は何ですか:
1) を設定してobject_id value
、MyModel
2
) fetch
MyCollection でトリガーし、
3) ?*でrender
関数をmyView
トリガーするか、私の目標をアクティブにしますか?
PS:
私の基本的なコードは次のようになります。
// My View
define([
"js/collections/myCollection",
"js/models/myFeed"
], function (myCollection, MyModel) {
var MyView = Backbone.View.extend({
initialize: function () {
var myModel = new MyModel();
_.bindAll(this, "render");
myModel.set({
object_id: this.options.object_id
}); // here I get an error: Uncaught TypeError: Object function (){a.apply(this,arguments)} has no method 'set'
}
});
return MyView;
});
// MyCollection
define([
"js/models/myModel"
], function (MyModel) {
var MyCollection = Backbone.Collection.extend({
url: function () {
return "http://localhost/movies/" + myModel.get("object_id");
}
});
return new MyCollection
});
//MyModel
define([
], function () {
var MyModel = Backbone.Model.extend({
});
return MyModel
});