2012-06-20 194 views
0

我刚刚开始学习Backbone.js。如何创建一个骨干模型以下使用情况Backbone.js嵌套模型

  • 国家:[0-N]
    • 县:[0-N]
      • 城市: [0-n]的
        • 名:
        • 公园[0-n]的
          • 名称:

任何帮助将是极大的赞赏。 这是我试图到目前为止

window.Team = Backbone.Model.extend({ 
     initialize: function(){ 
      this.id = this.get('id'); 
     } 
     }); 
     window.Teams = Backbone.Collection.extend({model:Team}); 

     window.Ward = Backbone.Model.extend({ 
     initialize: function(){ 
      this.name = this.get('name'); 
      this.code = this.get('code'); 
      this.teams = new Teams(this.get('teams')); 
     } 
     }); 

     window.Wards = Backbone.Collection.extend({model:Ward}); 

     window.LGA = Backbone.Model.extend({ 
     initialize: function(){ 
      this.name = this.get('name'); 
      this.wards = new Wards(this.get('wards')); 
     } 
     }); 

     window.LGAs = Backbone.Collection.extend({model:LGA}); 

     window.State = Backbone.Model.extend({ 
     initialize: function(){ 
      this.name = this.get('name'); 
      this.lgas = new Wards(this.get('lgas')); 
     } 
     }); 

     window.States = Backbone.Collection.extend({model:State, 

     initialize: function(){ 
      _.bindAll(this, 'fetch_success'); 
      this.bind('change', this.fetch_success); 
     }, 

     url: function(){ 
      return "data.json" 
     }, 

     fetch_success:function(){ 
      var data = Backbone.Syphon.serialize(someViewWithAForm); 
      var model = new Backbone.Model(data); 
     } 
     }); 

感谢

+0

显示我们http://whathaveyoutried.com,请。这显示了你到目前为止所拥有的东西,并且你没有在免费的代码之后。 – rcdmk

+0

更新了我试过的代码 –

回答

2

取决于你如何建立/获取你的模型,你可能有兴趣在Backbone-relational,它可以让你定义模型之间的关系。

从文档:

Person = Backbone.RelationalModel.extend({ 
    relations: [ 
     { 
      type: 'HasMany', 
      key: 'jobs', 
      relatedModel: 'Job', 
      reverseRelation: { 
       key: 'person' 
      } 
     } 
    ] 
});