2014-04-03 89 views
0

我想從具有數組的子陣列的JSON中檢索我的模型,並且變得瘋狂。對象被填充,但不與預期的模型一起使用。我設法讓第一級的工作很好,但這個技巧似乎不適用於第2級。任何替代方式這樣做將不勝感激。我對Backbone非常瞭解。如何使用backbone.js中的子集合投射集合

代碼:

(function() { 
    "use strict"; 
     APP.Models.User = Backbone.Model.extend({ 
     initialize: function() { 
      var listener = function() { console.log('myUser Updated'); }; 
      this.bind("changes", listener); 
     },   
     url: myurl, 
     parse: function(response){ 
      response.result.contents = new APP.Collections.Library(response.result.contents); 
      response.result.contents.forEach(function(content) {   
       content.set('contacts', APP.Collections.Contacts(content.get('contacts'))); 
      });     
      return response.result; 
     }       
     }); 
}()); 

JSON:

{ 
    "status": 1, 
    "result": { 
     "contents": [ 
      { 
       "contentId": "1", 
       "title": "test 1", 
       "contacts": [ 
        { 
         "nickName": "contact 1", 
         "phoneNumber": "11111" 
        }, 
        { 
         "nickName": "contact 2", 
         "phoneNumber": "22222" 
        }, 
        { 
         "nickName": "contact 3", 
         "phoneNumber": "33333" 
        } 
       ] 
      }, 
      { 
       "contentId": "2", 
       "title": "test 2", 
       "contacts": [ 
        { 
         "nickName": "Contact 4", 
         "phoneNumber": "44444" 
        }, 
        { 
         "nickName": "Contact 5", 
         "phoneNumber": "5555" 
        } 
       ] 
      } 
     ] 
    } 
} 

回答

0

好吧...我傻,我是缺少關鍵字new APP.Collection.Contacts前()。無論如何,它似乎不是最優雅的方式。任何有關改進的建議都非常感謝。