2013-01-16 41 views
5

Tôi chỉ mới bắt đầu với Backbone và tôi đang gặp sự cố. Tôi có một thiết lập tốt đẹp. Đối với hầu hết các yêu cầu GET của tôi, tôi nhận được một bộ sưu tập các mô hình, nhưng đối với một bộ tôi đang tải dữ liệu quan hệ lồng nhau, tạo dữ liệu lồng nhau JSON.phân tích cú pháp lồng nhau json với bộ sưu tập xương sống

Trong Backbone Tôi đã sau Models:

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

và những bộ sưu tập

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' 
}); 

Tôi tải dữ liệu như 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": [] 
        } 
       ] 
       } 
      ] 

Làm thế nào tôi có thể nhận được Sequences, LayersItems vào bộ sưu tập của tôi?

+0

Bạn có muốn phân tích ba bộ sưu tập riêng biệt, mỗi bộ sưu tập có chứa tất cả các thực thể không, ví dụ 'Lớp' từ tất cả 'Trình tự'? Hoặc một cấu trúc mô hình lồng nhau trong đó mỗi 'Sequence' có một bộ sưu tập phụ' Lớp'? – jevakallio

+0

Tôi cần ba bộ sưu tập riêng biệt. – lunacafu

Trả lời

8

Bạn có thể sử dụng một sự kết hợp của gạch của flattenpluck để làm điều này gọn gàng:

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); 

Nếu bạn không muốn Trình tự và Layers để chứa các đối tượng con của họ, ghi đè Model.parse để cắt chúng. Ví dụ:

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

Và khởi tạo/thêm bộ sưu tập với các tùy chọn parse:true:

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

Et vân.

Các vấn đề liên quan