gpt4 book ai didi

javascript - Backbone.js 嵌套模型

转载 作者:行者123 更新时间:2023-12-02 19:34:13 25 4
gpt4 key购买 nike

我刚刚开始学习 Backbone.js。如何为以下用例创建 Backbone 模型

  • 状态:[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);
}
});

谢谢

最佳答案

根据您构建/获取模型的方式,您可能会对 Backbone-relational 感兴趣,它允许您定义模型之间的关系。

来自文档:

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

关于javascript - Backbone.js 嵌套模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11124054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com