无法获得观看/收藏展示?

时间:2013-12-04 01:44:59

标签: javascript jquery backbone.js

我似乎无法获得测试backbone.js app工作,我不知道我做错了什么。

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

1 个答案:

答案 0 :(得分:1)

我修改了你的代码,这就是我想出来的。

// 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
});

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