Javascript Backbone.js嵌套模型
我刚刚开始学习Backbone.js。如何为以下用例创建主干模型Javascript Backbone.js嵌套模型,javascript,backbone.js,Javascript,Backbone.js,我刚刚开始学习Backbone.js。如何为以下用例创建主干模型 国家:[0-n] 名字 县:[0-n] 名字 城市:[0-n] 姓名: 公园[0-n] 姓名: 任何帮助都将不胜感激。 这就是我目前所尝试的 window.Team = Backbone.Model.extend({ initialize: function(){ this.id = this.get('id'); } }); wind
- 国家:[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);
}
});
谢谢根据您构建/获取模型的方式,您可能会对它感兴趣,它允许您定义模型之间的关系 从文档中:
Person = Backbone.RelationalModel.extend({
relations: [
{
type: 'HasMany',
key: 'jobs',
relatedModel: 'Job',
reverseRelation: {
key: 'person'
}
}
]
});
请给我们看看。这显示了你到目前为止所拥有的,并且你不是在追求免费的
code
家伙。用我尝试过的代码更新