似乎您的数据是模型,并且每个 type_{n} 都是集合,但是在骨干中我们不能这样写。如果我是你,我会做这样的事情:
coll = [
{}, {}, {}
],// first collection
[
{}, {}
]// second collection
因为它将第二个集合添加到第一个集合,所以它们被视为一个集合。所以我将从这两个集合中制作一个模型。简而言之,通过代码自己做:
var SimpleModel = Backbone.Model.extend({});
var SimpleCollection = Backbone.Collection.extend({ model: SimpleModel});
var SubModel = Backbone.Model.extend({
default: {
coll: new SimpleCollection()
}
});
var SubCollection = Backbone.Collection.extend({ model: SubModel});
var ParentModel = Backbone.Model.extend({});
我使用浏览器命令行中的以下代码来检查它,也许它对你也有帮助:
var s1 = new SimpleModel({"name":"n1"});
var s2 = new SimpleModel({"name":"n2"});
var s3 = new SimpleModel({"name":"n3"});
var c1 = new SimpleCollection();
c1.add(s1);
c1.add(s2);
var c2 = new SimpleCollection([s1,s2,s3])
var ss1 = new SubModel({"col" : c1});
var ss2 = new SubModel({"col" : c2});
var cc1 = new SubCollection([ss1,ss2]);
var cc2 = new SubCollection([ss1]);
var p1 = new ParentModel({"type_1": cc1, "type_2" : cc2});
JSON.stringify(p1);