如何解决如何在骨干.js中查看模型列表
| 这是我的content.js,其中我正在使用bones.js呈现内容。 // Our basic **Content** model has `content`,`order`,and `done` attributes.
var Content = Backbone.Model.extend({
// If you don\'t provide a Content,one will be provided for you.
EMPTY: \"empty Content...\",// Ensure that each Content created has `content`.
initialize: function() {
}
});
var ContentCollection = Backbone.Collection.extend({
model : Content
});
// Create our global collection of **Todos**.
window.Contents = new ContentCollection;
// The DOM element for a Content item...
var ContentView = Backbone.View.extend({
//... is a list tag.
tagName: \"li\",events: {
\"click .content\": \"open\"
},// a one-to-one correspondence between a **Content** and a **ContentView** in this
// app,we set a direct reference on the model for convenience.
initialize: function() {
_.bindAll(this,\'render\',\'close\');
this.model.bind(\'change\',this.render);
this.model.view = this;
},// Re-render the contents of the Content item.
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
这是我制作内容列表并渲染它们的方式。
for(var i=0; i<data.length; i++) {
var content = new Content(data[i]);
var templ=_.template($(\'#tmpl_content\').html());
var view = new ContentView({model: content});
view.template=templ;
$(\"#content\").append(view.render().el);
}
我的问题是如何获得竞争模型清单。
正如我创建的收藏
var ContentCollection = Backbone.Collection.extend({
model : Content
});
// Create our global collection of **Todos**.
window.Contents = new ContentCollection;
因此,当我观看Contents
时,它会显示长度0和型号[]。
如何在收藏中添加竞争力。或如何在骨干.js中查看模型列表
解决方法
您需要在Collection.add(models)包含任何内容之前。
,您还可以在集合上指定一个URL(应返回模型的JSON数组),然后执行window.Contents.fetch()。骨干网将自动填充您的集合中指定的模型(内容),并将其自动添加到您的集合中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。