【发布时间】:2024-01-19 12:41:01
【问题描述】:
我正在开发一个使用 ember.js 和沙发数据库后端的应用程序。到目前为止,我使用 ember-resource 作为数据库驱动程序,但我正在考虑切换到 ember-data,因为这似乎更可持续。
由于我使用的是沙发数据库,因此我使用的是 Couch DB-Adapter。
我的数据库中的文档包含完整的对象结构,所以我必须在数据库驱动程序中指定嵌入的对象。
虽然我将我的子对象指定为嵌入的,但 ember-data 似乎通过单独的请求获取这些对象,而不是仅仅将它们从主 json 中取出。
我的对象定义如下:
App.UserProfile = DS.Model.extend({
type: DS.attr('string'),
fullname: DS.attr('string'),
email: DS.attr('string'),
pictureUrl: DS.attr('string'),
social: DS.hasMany('App.SocialWebAccount', { embedded: true }),
.....
});
App.SocialWebAccount = DS.Model.extend({
profile: DS.belongsTo('CaiMan.UserProfile'),
site: DS.attr('string'),
account: DS.attr('string'),
.....
});
服务器数据是这样的:
{
"_id": "thoherr",
"_rev": "55-d4abcb745b42fe61f1a2f3b31c461cce",
"type": "UserProfile",
"fullname": "Thomas Herrmann",
"email": "test@thoherr.de",
"pictureUrl": "",
"social": [
{
"site": "socialFacebook",
"account": "thoherr"
},
{
"site": "socialXing",
"account": "Thomas_Herrmann7"
},
{
"site": "socialEmail",
"account": "test@thoherr.de"
}
]
}
加载后,UserProfile 确实包含我的社交数据的 ArrayProxy,它由三个条目填充,但它们都是未定义的,而不是 SocialWebAccount 的实例!
如果我尝试访问此数组,ember-data 似乎会执行单独的数据库访问以获取数据,这会导致错误,因为沙发 DB 适配器访问 _id 字段,该字段在未定义中不可用。 ...
我错过了什么?
我认为“嵌入”标志表明数据已经在 json 中并且可以从 json 中实例化对象?
为什么 ember-data 会尝试获取嵌入的数据?
有什么提示吗?
【问题讨论】:
标签: ember.js ember-data