骨干关系型,使用mysql数据将模型表示为表行
Backbone relational, working with mysql data representing models as table rows
假设我们在mysql中有两个表:cars和engines。
汽车表行将具有以下结构:
id
:intengineId
:int(engines.id
列的外键)brand
:字符串
发动机表行这一行:
- CCD_ 5:int
type
:string(假设我们有电动和柴油发动机)
所以,我想从这些表中检索所有数据,在客户端创建模型集合,并最终显示具有合并结果的表:
- 汽车id
- 品牌名称
- 发动机类型
所以,我试过很多例子,但我不明白我做错了什么。你能帮忙吗?
// Create car model
window.Car = Backbone.RelationalModel.extend({
});
// Create engine model
window.Engine = Backbone.RelationalModel.extend({
relations: [{
type: 'HasMany',
key: 'cars',
relatedModel: 'Car',
reverseRelation: {
key: 'engine',
includeInJSON : 'engineId',
}
}]
});
// Create engine instance
var engine = new window.Engine({
id : 1,
type : 'electric',
});
// Create car instance
var car = new window.Car({
id : 1,
brand : 'Toyota',
engineId : 1,
});
// i expect to get 'electric'
console.log(car.getRelation('engine').type);
试试这个:
// Create car model
var Car = Backbone.RelationalModel.extend({
});
// Create engine model
var Engine = Backbone.RelationalModel.extend({
relations: [{
type: 'HasMany',
key: 'cars',
relatedModel: Car,
reverseRelation: {
key: 'engine'
}
}]
});
// Create engine instance
var engine = new Engine({
id : 1,
type : 'electric'
});
// Create car instance
var car = new Car({
id : 1,
brand : 'Toyota',
engine : 1
});
// get 'electric'
console.log(car.get('engine').get('type'));
相关文章:
- 在VanillaJS中模拟模型双向数据绑定
- 使用 ng 模型获取数据时遇到问题
- AngularJS从另一个数据模型访问数据模型
- 使用主干上的新数据更改模型的数据
- 使用 Ember.js 在模板中显示模型 JSON 数据
- 使用 Knockoutjs 显示视图模型中数据的更好方法
- 如何从下拉列表中选择等效于 AngularJS 中的 ng 模型的数据
- 骨干关系型,使用mysql数据将模型表示为表行
- AngularJS ng模型形式由ng驱动,通过UI模型描述数据如何重复
- Qml嵌套列表模型附加数据
- 如何使用angular将模型的数据发送到文本字段
- 如何在angularjs中提取和发送具有多个不同模型的数据
- 骨干视图模型vs(数据)模型
- 为了有效地搜索和比较字符串,我应该如何表示数据?
- 使用 Ember.js 加载属于父模型的数据
- Knockout js从其他视图模型收集数据
- 为模型指定数据属性
- 向下滚动加载引导程序模型的数据(Lazy Load)
- 从引导程序模型传递数据
- 用于POST保存的主干模型集数据