在 Ember.js 中从belongsTo 关联中查找记录 [英] Find record from belongsTo association in Ember.js
本文介绍了在 Ember.js 中从belongsTo 关联中查找记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 Ember 模型中获取关联记录?或者:如何从Promise对象中获取记录?
How can I get the associated record from an Ember model? Or: how to get the record from the Promise Object?
客户模型
Docket.Customer = DS.Model.extend({
name: DS.attr('string'),
initial: DS.attr('string'),
description: DS.attr('string'),
number: DS.attr('string'),
archived: DS.attr('boolean'),
projects: DS.hasMany('project',{ async: true })
});
项目模型
Docket.Project = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
number: DS.attr('string'),
archived: DS.attr('boolean'),
customer: DS.belongsTo('customer', { async: true })
});
查找方法
var project = this.store.find('project', id).then(function(data) {
console.log(data.get('customer').toString());
});
控制台输出
<DS.PromiseObject:ember654>
JSON 响应
{"projects":[
{
"id":1,
"name":"test",
"number":"a310",
"description":null,
"archived":false,
"customer_id":22
}
]};
推荐答案
用另一个然后就搞定 :)
use another then on the get :)
var project = this.store.find('project', id).then(function(data) {
data.get('customer').then(function(c){
console.log(c);
}
});
这篇关于在 Ember.js 中从belongsTo 关联中查找记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文