可以't无法显示视图/集合

Can't get a views/collection to show?

本文关键字:显示 视图 集合 可以      更新时间:2024-02-14

我似乎无法让测试backbone.js应用程序正常工作,我也不知道自己做错了什么。

http://jsbin.com/iwigAtah/1/edit

我对您的代码做了一点修改,下面是我的想法。

// Backbone Objects
var Item = Backbone.Model.extend({});
var List = Backbone.Collection.extend({
    model: Item
});
var ListView = Backbone.View.extend({
  initialize: function(options){
    // collection is now passed in as an argument
    // this line isn't necessary but makes it easier to understand what is going on
    this.collection = options.collection;
    console.log(this.collection.models);
  }
});

// Create a new empty collection
var test = new List();
// Create a new item model and add it to the collection
var testItem = new Item();
test.add(testItem);
// Create a view with your new collection
var g = new ListView({
  collection: test
});

您遇到的主要问题是,您实际上并没有将模型添加到集合中。