为什么不在Backbone.js中设置集合?

时间:2011-12-04 17:01:39

标签: javascript jquery backbone.js

以下代码有什么问题?


var Item = Backbone.Model.extend({
    defaults: {

    }
});

var List = Backbone.Collection.extend({
    model: Item
});

var collection = new List({'variable':this}); //collection is NOT null!
var result = new Item({'collection':collection}); //result.collection is undefined! why?

1 个答案:

答案 0 :(得分:3)

将您的项目添加到列表

var collection=new List,
result=new Item({ name: 'my item'});
collection.add( result );

collection.create({ name: 'my item'});