【发布时间】:2016-04-22 03:20:27
【问题描述】:
我在 ember-data 中的“user.js”模型中有以下代码:
export default DS.Model.extend({
organization: DS.belongsTo('organization'),
//other stuff
});
网站的 CRUD 按预期工作,在 MongoDB 中,我可以看到用户的组织字段如下:
"organization" : ObjectId("571974742ce868d575b79d6a"),
但是,我不确定这是我的代码中的错误还是我不了解 Ember-data 的工作原理,我无法像这样从模型挂钩访问该 ID:
model(){
return this.store.findRecord("user", this.get("session.currentUser.id"))
.then(user => this.store.findRecord("location", {organization: user.organization}));
}
如果我去 Ember 检查器观察 User 对象的 belongsTo 属性,我会看到:
organization: <(subclass of Ember.ObjectProxy):ember956>
但是点击我看到content: null
我做错了什么?会不会是服务器端错误?
编辑包括来自服务器对上述 findRecord("user") 调用的 JSON 响应:
{
"links":{
"self":"/users/5719749a2ce868d575b79d6b"
},
"included":[
{
"type":"organizations",
"id":"571974742ce868d575b79d6a",
"links":{
"self":"/organizations/571974742ce868d575b79d6a"
},
"attributes":{
"creation-date":"2016-04-22T00:46:44.779Z"
}
}
],
"jsonapi":{
"version":"1.0"
},
"data":{
"type":"users",
"id":"5719749a2ce868d575b79d6b",
"links":{
"self":"/users/5719749a2ce868d575b79d6b"
},
"attributes":{
"email":"danthwa@gmail.com",
"first-name":"Daniel",
"last-name":"Thompson",
"registration-date":"2016-04-22T00:47:22.534Z"
},
"relationships":{
"organization":{
"type":"organizations",
"id":"571974742ce868d575b79d6a"
}
}
}
}
【问题讨论】:
-
ObjectProxy 是一个可能无法解析的对象,您的组织是否应该是异步的?
-
即您的服务器的用户 json 有效负载是否也包含组织信息,还是仅包含组织的标识符?
-
它包含一个关系键:"relationships":{"organization":{"type":"organizations","id":"571974742ce868d575b79d6a"}}。所以我想这只是一个标识符?我需要做的就是能够访问我认为的那个 id,因为我正在尝试基于从不同模式到该组织的链接来填充模型。
-
我不熟悉 jsonapi 规范或它在 Ember Data 中的实现方式(我个人认为规范是臃肿的垃圾)。这是我们可以使用的 jsbin:emberjs.jsbin.com/vodutiteku/edit?html,js,output
-
在我看来,似乎关系没有正确建立,我认为组织内部的类型和 id 需要在数据对象中(参见 jsbin)