Backbone:在模型中存储集合

时间:2016-03-27 22:06:42

标签: javascript backbone.js

我正在尝试将Backbone Collection存储在Backbone模型中,但它无法正常工作。而不是实际存储Collection对象,而是存储数组。

doit

是否出现了问题,或者这是否与开箱即用的Backbone无关?

1 个答案:

答案 0 :(得分:0)

你的例子似乎没了。这是你想要做的吗?这是将模型存储在集合中的示例,然后是模型中的集合:

var ModelWithCollection = Backbone.Model.extend({
    defaults: {
        typicalCollection: null
    }
});

var TypicalModel = Backbone.Model.extend({});

var TypicalCollection = Backbone.Collection.extend({
    model: TypicalModel 
});
//create some model instances
var typicalModelA = new TypicalModel();
var typicalModelB = new TypicalModel();
//create typical collection instance and add models
var myCollection = new TypicalCollection([typicalModelA, typicalModelB]);

//now create a model with a collection
var modelWithCollection = new ModelWithCollection({typicalCollection:myCollection});