기본 모델은 children
이고, 필요하면 new Collection
으로 설정하면 간단한 모델을 만들 수 있습니다. 주요 부분은 JSON 데이터에서 모델로로드하는 것입니다.
var MyModel = Backbone.Model.extend({
});
var MyCollection = Backbone.Collection.extend({
model: MyModel,
load: function(data) {
for(var i in data) {
if (_.isString(data[i])) {
this.add(new MyModel({
title: data[i]
}));
} else {
var collection = new MyCollection();
collection.load(data[i]);
this.add(new MyModel({
title: i,
children: collection
}));
}
}
}
});
var collection = new MyCollection();
collection.load({
'item1': {
'item a': ['item i', 'item ii'],
0: 'item b',
1: 'item c'
},
0: 'item2',
1: 'item3'
});
console.log(collection);
만이 "0"과이 예 항목에서를
"1"키 컬렉션의 beggining에 가고있다,하지만 당신은 아이디어를 볼 수 있습니다.
var Node = Backbone.Model.extend({
parse: function(data) {
this.nodes = new Nodes(data.nodes, {parse: true});
return _.omit(data, 'nodes');
}
});
var Nodes = Backbone.Collection.extend({
model: Node
});
var c = new Nodes(data_structure, {parse: true});
// parse: true is only needed if you pass the data as an argument
Node.parse
데이터의 해시에서 nodes
속성을 추출 : 데이터 구조를 가정
는