2013-01-16 4 views
5

방금 ​​Backbone으로 시작하고 있으며 문제가 있습니다. 나는 즐거운 안정된 체제를 가지고있다. 대부분의 내 GET 요청에 대해 하나의 모델 모음을 받지만 중첩 된 관계형 데이터를로드하는 중첩 된 JSON을 생성합니다.백본 컬렉션에 중첩 된 json을 구문 분석합니다.

Backbone에서

나는 Models을 다음 있습니다 :

App.Models.Sequence = Backbone.Model.extend({}); 
App.Models.Layer = Backbone.Model.extend({}); 
App.Models.Item = Backbone.Model.extend({}); 

및 이러한 컬렉션을

App.Collections.Sequences = Backbone.Collection.extend({ 
    model: App.Models.Sequence, 
    url: '/api/sequences/' 
}); 

App.Collections.Layers = Backbone.Collection.extend({ 
    model: App.Models.Layer, 
    url: '/api/layers' 
}); 


App.Collections.Items = Backbone.Collection.extend({ 
    model: App.Models.Item, 
    url: '/api/items' 
}); 

내가 JSON로 데이터를로드 :

[ 
       { 
       "id": "1", 
       "project_id": "8", 
       "name": "Seq1", 
       "layers": [ 
        { 
        "id": "1", 
        "name": "Layer11", 
        "sequence_id": "1", 
        "items": [ 
         { 
         "id": "1000000", 
         "layer_id": "1", 
         "itemtype_id": "1", 
         "position": "0" 
         }, 
         { 
         "id": "1000001", 
         "layer_id": "1", 
         "itemtype_id": "2", 
         "position": "0" 
         }, 
         { 
         "id": "1000002", 
         "layer_id": "1", 
         "itemtype_id": "2", 
         "position": "0" 
         }, 
         { 
         "id": "1000003", 
         "layer_id": "1", 
         "itemtype_id": "4", 
         "position": "0" 
         } 
        ] 
        }, 
        { 
        "id": "2", 
        "name": "Layer12", 
        "sequence_id": "1", 
        "items": [ 
         { 
         "id": "1000004", 
         "layer_id": "2", 
         "itemtype_id": "1", 
         "position": "0" 
         }, 
         { 
         "id": "1000005", 
         "layer_id": "2", 
         "itemtype_id": "2", 
         "position": "0" 
         }, 
         { 
         "id": "1000006", 
         "layer_id": "2", 
         "itemtype_id": "3", 
         "position": "0" 
         }, 
         { 
         "id": "1000007", 
         "layer_id": "2", 
         "itemtype_id": "4", 
         "position": "0" 
         } 
        ] 
        }, 
        { 
        "id": "3", 
        "name": "Layer13", 
        "sequence_id": "1", 
        "items": [ 
         { 
         "id": "1000008", 
         "layer_id": "3", 
         "itemtype_id": "1", 
         "position": "0" 
         }, 
         { 
         "id": "1000009", 
         "layer_id": "3", 
         "itemtype_id": "4", 
         "position": "0" 
         }, 
         { 
         "id": "1000010", 
         "layer_id": "3", 
         "itemtype_id": "5", 
         "position": "0" 
         } 
        ] 
        } 
       ] 
       }, 
       { 
       "id": "2", 
       "project_id": "8", 
       "name": "Seq2", 
       "layers": [ 
        { 
        "id": "4", 
        "name": "Layer21", 
        "sequence_id": "2", 
        "items": [] 
        }, 
        { 
        "id": "5", 
        "name": "Layer22", 
        "sequence_id": "2", 
        "items": [] 
        } 
       ] 
       }, 
       { 
       "id": "3", 
       "project_id": "8", 
       "name": "Seq3", 
       "layers": [ 
        { 
        "id": "6", 
        "name": "Layer31", 
        "sequence_id": "3", 
        "items": [] 
        }, 
        { 
        "id": "7", 
        "name": "Layer32", 
        "sequence_id": "3", 
        "items": [] 
        } 
       ] 
       } 
      ] 

는 어떻게 얻을 수 Sequences, Layers 및 01 내 컬렉션에?

+0

3 개의 개별 모음을 구문 분석하고 싶습니다. 각 모음에는 예를 들어 모든 항목이 들어 있습니다 (예 : 모든 '시퀀스'의 '레이어')? 또는 각'Sequence'가'Layers' 서브 콜렉션을 갖는 중첩 모델 구조입니까? – jevakallio

+0

3 개의 별도 컬렉션이 필요합니다. – lunacafu

답변

8

당신은 깔끔하게 이렇게 밑줄의 flattenpluck의 조합을 사용할 수 있습니다

var data = { /* your JSON data */ }; 
var allSequences = _.clone(data); 
var allLayers = _.flatten(_.pluck(allSequences, 'layers')); 
var allItems = _.flatten(_.pluck(allLayers, 'items')); 

var sequences = new App.Collections.Sequences(allSequences); 
var layers = new App.Collections.Layers(allLayers); 
var items = new App.Collections.Items(allItems); 

당신이 시퀀스와 레이어 자녀 객체를 포함하지 않으려면

, 그들을 트림 Model.parse 우선합니다. 예를 들어 :

App.Models.Sequence = Backbone.Model.extend({ 
    parse: function(attrs) { 
     delete attrs.layers; 
     return attrs; 
    } 
}); 

그리고 초기화 /를 parse:true 옵션을 사용하여 컬렉션을 추가

var sequences = new App.Collections.Sequences(allSequences, {parse:true}); 

기타 등등을.

관련 문제