백본 1.1을 사용 중이며 3-5 레벨의 트리 탐색 기능으로 컬렉션을 만들려고합니다.백본 컬렉션 트리 생성
내 코드의 단순화 된 버전은 다음과 같습니다. 나의 이해이 올바르게 깊은 수준의 어린이 컬렉션을 만들어야에서
var treeItem = Backbone.Model.extend({
defaults : {
'label' : '',
'children' : null
},
initialize : function() {
console.log('model init');
if (_.isArray(this.get('children'))) {
this.set({children : new treeItemCollection(this.get('children'))});
}
},
});
var treeItemCollection = Backbone.Collection.extend({
model: treeItem
});
var myTree = new treeItemCollection([
{ "label" : "first", "id": 1 },
{ "label" : "second", "id": 1, "children":
[
{ "label" : "second.first", "id" : 21 },
{ "label" : "second.second", "id" : 22 },
{ "label" : "second.third", "id" : 22, "children" : [
{ "label" : "third.first", "id" : 31 },
{ "label" : "third.second", "id" : 32 }
] }
]
}
]);
(객체 따라서 제대로 깊은 수준을 생성하여 구성 할 때, 내 이해로 초기화가 호출되어야한다).
어떤 이유로 이럴 것 같지 않습니다. 두 번째 레벨 (예 : myTree.models[0].get('children')
)은 treeCollection 유형의 컬렉션이지만 3 번째 레벨 (myTree.models[0].get('children').models[0].get('children')
)은 매개 변수 객체의 JSON을 똑바로 위로 한 것입니다.
제게 두 번째 레벨은 괜찮습니다. ,하지만 세 번째는 아닙니다. console.log는 initialize()
입니다. 확인하면 6 번이 아니라 4 번 트리거됩니다.
왜 3 단계로 변환되지 않는지 이해하려고합니다. 컬렉션
btw, 어떤 이유로 든 저의 작품이 저에게 효과적입니다. json에 ID가 중복 된 경우는 예외입니다. 그것이 문제를 일으키는 지 궁금합니다. –