我遇到了和你一样的情况,我并不总是希望最初引导我的数据(特别是如果它来自第三方api调用)。我还没有遇到任何这样做的教程,但通过文档查看其实非常简单。你只需要在你的集合和渲染上绑定'重置'事件(当整个集合被重新填充时触发)。下面是一个简单的例子:
my_application。JS
window.MyApplication = {
Models: {},
Collections: {},
Views: {},
Routers: {},
init: function() {
// Start the data loading
this.someItems = new MyApplication.Collections.Items();
this.someItems.fetch();
// Start rendering the UI before the data is ready
new MyApplication.Routers.Items(this.someItems);
Backbone.history.start();
}
};
路由器/ items_router.js
MyApplication.Routers.Items = Backbone.Router.extend({
routes: {
"" : "index"
},
initialize: function(collection) {
this.collection = collection;
},
index: function() {
var view = new MyApplication.Views.ItemsIndex({ collection: this.collection });
$('.items').html(view.render().el);
}
});
的意见/项目/ items_index.js
MyApplication.Views.ItemsIndex = Backbone.View.extend({
initialize: function() {
_.bindAll(this, "render");
// Once the collection is fetched re-render the view
this.collection.bind("reset", this.render);
},
render: function() {
console.log(this.collection.length);
// Render content
return this;
}
});
有关使用Backbone进行引导的更多信息,请参阅官方文档:http://documentcloud.github.com/backbone/#FAQ-bootstrap –
@Rilely - 我已阅读文档。但谢谢:) – UpTheCreek
数据被转储到js。 –