backbone.jsを使用して私の最初のプロジェクトに取り組んでいます。 Playのフロントエンドでなければなりません! JSONインターフェイスを持つアプリケーション。ここに私のJSコードJSON Response with Backbone.js
var api = 'http://localhost:9001/api'
// Models
var Node = Backbone.Model.extend();
// Collections
var Nodes = Backbone.Collection.extend({
model: Nodes,
url: api + '/nodes',
});
// Views NODE
var NodeView = Backbone.View.extend({
el: $("#son_node_elements"),
render: function(){
var source = $("#son_node_item_templ").html();
var template = Handlebars.compile(source);
$(this.el).append(template(this.model.toJSON()));
return this;
}
});
var NodeListView = Backbone.View.extend({
initialize: function(){
_.bindAll(this, "render");
this.collection = new Nodes();
this.collection.bind("change",this.render);
this.collection.fetch();
this.render();
},
render: function(){
_(this.collection.models).each(function(item){
var nodeView = new NodeView({model: item});
$(this.el).append(nodeView.render().el);
}, this);
}
});
Nodes = new NodeListView({el:$('#son_nodes')});
の一部だ私の問題は、this.render()
が呼び出されたときに、this.collection.fetch()
はまだ行われていないとthis.collection
をレンダリングするanithing含まれていないことです。私がthis.render()
にブレークポイントを設定したときには、すべてが正常に動作します(例えば、firebugを使用)。this.render()
はすぐに呼び出されません。私は、私のアプリのAPIではなく、ローカルのJSONファイルにアクセスするときと全く同じ結果を得ます。どのようにこの問題を処理するための任意の提案?
this.collection.fetch()の実行中にエラーが発生した場合はどうなりますか? – c4urself
成功関数とエラー関数をfetch関数に渡すことができます –