2015-08-28 98 views
1

我有以下JSON。在骨幹模型中存儲部分嵌套JSON

[ 
     { 
     "FirstName": "John", 
     "SecondName": "Steve", 
     "Children":{ 
           "results":[ 
            { 
             "name":"Sue Anderson" 
            }, 
            { 
             "name":"Jeff Anderson" 
            }, 
            { 
             "name":"Mary Sanders" 
            } 

            ] 
      } 

     }, 

     { 
     "FirstName": "gary", 
     "SecondName": "Steve", 
     "Children":{ 
           "results":[ 
            { 
             "name":"nick" 
            }, 
            { 
             "name":"Jerry" 
            }, 
            { 
             "name":"tom" 
            } 

            ] 
      } 

     } 


] 

我有一個骨幹模型規範來表示它。

var FamilyData= Backbone.Model.extend({ 
     defaults: { 
      FirstName: '', 
      SecondName:'', 
      Children:'' 
     } 
    }); 

如何將該兒童部分作爲數組存儲在模型中?

回答

1

似乎你想要的是將Children.results改爲放入Children中。

所以添加到模型解析方法重寫:

var FamilyData= Backbone.Model.extend({ 
     defaults: { 
      FirstName: '', 
      SecondName:'', 
      Children:'' 
     }, 
     parse: function(response) { 
      response.Children = response.Children.results 
      return response; 
     } 

    }); 

這應該導致生成的FamilyData.Children包含的內容的JSON曾在Children.results