我将查询在响应中返回嵌入对象的API,因此我使用fixture适配器模拟这个。我的数据如下:
App.Deal.FIXTURES = [
{id: 1, name: 'Deal 1', closed_time: '2012-04-01', value_in_cents: 342345, company: { id: 1, name: 'Google' }, user: { id: 2, full_name: 'Bob Smith' } },
{id: 2, name: 'Deal 2', closed_time: '2012-04-01', value_in_cents: 342345, company: { id: 1, name: 'Google' }, user: { id: 2, full_name: 'Bob Smith' } }
]
这是我的模特:
App.Deal = DS.Model.extend({
name: DS.attr('string'),
value_in_cents: DS.attr('number'),
closed_time: DS.attr('date'),
user: DS.belongsTo('App.User'),
company: DS.belongsTo('App.Company')
});
App.User = DS.Model.extend({
full_name: DS.attr('string')
});
App.Company = DS.Model.extend({
name: DS.attr('string')
});
我不确定如何获取user
和company
个对象。我通过控制台看到数据:
App.Deal.find(1)
Class {store: Class, clientId: 2, _relationshipChanges: Object, _changesToSync: Object, id: "1"…}
__ember1365792240874: "ember329"
__ember1365792240874_meta: Meta
_changesToSync: Object
_data: Object
attributes: Object
belongsTo: Object
company: Object
id: 1
name: "Google"
__proto__: Object
但我做不到App.Deal.find(1).get('user')
。我错过了什么吗?
答案 0 :(得分:3)
以下设置有效:
App.Adapter = DS.FixtureAdapter.extend();
App.Adapter.map('App.Deal', {
user: {embedded: 'load'},
company: {embedded: 'load'}
});
App.User = DS.Model.extend({
full_name: DS.attr('string')
});
App.Company = DS.Model.extend({
name: DS.attr('string')
});
App.Deal = DS.Model.extend({
name: DS.attr('string'),
value_in_cents: DS.attr('number'),
closed_time: DS.attr('date'),
user: DS.belongsTo(App.User, {embedded: 'load'}),
company: DS.belongsTo(App.Company, {embedded: 'load'})
});