2014-10-09 43 views
1

我不確定如何使用以下數據創建Backbone集合,並且不知道應該如何處理它。如何使用不平坦的數據創建Backbone集合

JSON中的items屬性對我來說很有意義,因爲它已經是一個數組了。但是有沒有一種很好的方法可以將其他信息提供給集合,或者保持其持久性以防需要在視圖中引用它?我應該如何處理集合的以下數據格式?

下面是一些示例數據和代碼:

{ 
"page": 1, 
"total": 1000, 
"items": [ 
    { 
     "title": "title of item 1", 
     "color": "green", 
     "type": [ 
      { 
       "isStandard": "Yes", 
       "size": "Large" 
      }, 
      { 
       "isStandard": "No", 
       "size": "Medium" 
      } 
     ], 
     "price": "9.99" 
    }, 
    { 
     "title": "title of item 2", 
     "color": "blue", 
     "type": [ 
      { 
       "isStandard": "No", 
       "size": "XXL" 
      }, 
      { 
       "isStandard": "No", 
       "size": "XXS" 
      } 
     ], 
     "price": "19.99" 
    }, 
    { 
     "title": "title of item 3", 
     "color": "red", 
     "type": [ 
      { 
       "isStandard": "No", 
       "size": "Small" 
      }, 
      { 
       "isStandard": "Yes", 
       "size": "Large" 
      } 
     ], 
     "price": "229.99" 
    } 
], 
"date": "Wed Oct 08 2014 21:04:05 GMT-0500 (CDT)" 
} 

var SomeModel = Backbone.Model.extend({}); 
 

 
var SomeCollection = Backbone.Collection.extend({ 
 
    model: SomeModel, 
 
    
 
    url: 'https://api.mongolab.com/api/1/databases/backbonecollectiontest/collections/backbonecollection?apiKey=qcVNgNb-s1X4WJkLeRDfykxqtMG-ezkC', 
 
    
 
    parse: function(response) { 
 
     // Returning only the items data. But this will be missing data in the response 
 
     // that is not included in the items property 
 
     return response[0].items; 
 
    } 
 
}); 
 

 
var SomeView = Backbone.View.extend({ 
 
    el: '.js-container', 
 
    
 
    initialize: function() { 
 
     this.collection = new SomeCollection(); 
 
     
 
     this.collection.fetch(); 
 
     
 
     this.listenTo(this.collection, 'sync', this.render); 
 
    }, 
 
    
 
    template: _.template($('#some-template').html()), 
 
    
 
    render: function() { 
 
     
 
     this.$el.html(this.template({collection: this.collection.toJSON()})); 
 
     
 
    } 
 
}); 
 

 
var someView = new SomeView();
<div class="js-container"> 
 
</div> 
 

 
<script type="text/template" id="some-template"> 
 
    <h3>How should I get the 'page', 'total', and 'date' properties in the data here?</h3> 
 
    <% _.each(collection, function(element, index) { %> 
 
     <p> 
 
     <%- element.title %>, 
 
     <%- element.color %>, 
 
     <% _.each(element.type, function(ele, i) { %> 
 
      <%- ele.isStandard %>, 
 
      <%- ele.size %>, 
 
     <% }); %> 
 
     <%- element.price %> 
 
     </p> 
 
    <% }); %> 
 

 
</script> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://underscorejs.org/underscore-min.js"></script> 
 
<script src="http://backbonejs.org/backbone-min.js"></script>

+0

目前尚不清楚您實際要求的內容,因爲您使用的語言非常模糊,如「獲取其他信息」,「保持其持久性」和「我應該如何處理......」。以需要解決方案的特定問題形式表達您的問題將有助於人們回答。否則,你的代碼看起來非常好。它運行嗎? – 2014-10-09 19:21:22

+0

@Tguguen我對不清楚的問題表示歉意。該代碼確實運行,但我不確定如何使用我提供的格式創建具有數據的平面模型和集合。我也想知道是否需要扁平模型以及其他人如何處理這種數據。 – Mdd 2014-10-10 01:53:40

+0

那麼我要說的第一件事就是創建平面模型並不是真的有必要。這是JSON的主要優點之一,它允許您簡單地序列化更復雜的數據對象。我很難找到對平面數據模型的需求。我認爲這種思維方式是舊式關係數據庫設計中過時的剩餘,其中一行數據總是以平坦的非標準化格式進行檢索。 – 2014-10-10 13:09:06

回答

1

看起來像你在這裏需要嵌套模式:

這裏是一個原型趕上理念,爲更多信息結帳this post

1)創建項目

var Item = Backbone.Model.extend({ 
    defaults: { 
     "title": "no-title", 
     "color": "no", 
     "type": [], 
     "price": "0" 
    } 
}); 

var ItemsCollection = Backbone.Collection.extend({ 
    model: Item 
}); 

2模型和集合)創建父模型,看起來像網頁你的情況:

var PageModel = Backbone.Model.extend({ 
    defaults: { 
     page: 0, 
     date: 'now', 
     total: 0, 
     items: [] 
    }, 
    initialize: function() { 
     var items = this.get('items'); 
     this.set('items', new ItemsCollection(items)); 
    } 
}); 

如果在JSON RESP的陣列,U可以創建PageCollection爲好。

+0

'this.items.set(new ItemsCollection(items));'不會產生您所期望的結果。 'this.items'指的是Model的**屬性**'items',而不是**屬性**。正確的方法是:this.set('items',new ItemsCollection(items))''。儘管目前還不清楚你的代碼中的'item'變量來自哪裏? – hindmost 2014-10-09 15:02:32

+0

@最後,感謝您的關注,我沒有編寫代碼,想法是用BB收集實例重新定義'items'屬性 – Evgeniy 2014-10-09 16:45:39

+0

@Evgeniy謝謝!雖然我還是有點迷路。讓我看看我是否遵循。 PageModel在初始化時創建一個ItemCollection。這裏是我一直在編輯的小提琴,用來嘗試記錄信息。當我記錄PageModel的結果時,我看到一個包含feteched數據的附加屬性。雖然我不確定這個額外的財產來自哪裏。 http://jsfiddle.net/ozt00tt2/ – Mdd 2014-10-10 01:46:39

相關問題