gpt4 book ai didi

javascript - 带有嵌入对象的 Ember.js 模型

转载 作者:行者123 更新时间:2023-11-28 20:33:02 25 4
gpt4 key购买 nike

我将查询一个在响应中返回嵌入对象的 API,因此我使用夹具适配器对此进行模拟。我的数据如下所示:

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')
});

我不确定如何获取 usercompany 对象。我通过控制台看到数据:

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')。我错过了什么吗?

最佳答案

以下设置有效:

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'})
});

关于javascript - 带有嵌入对象的 Ember.js 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15978774/

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