2014-10-09 25 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

相关问题