【发布时间】:2014-11-14 16:06:51
【问题描述】:
我的 Ember 应用程序与 API(通过 DS.ActiveModelAdapter 适配器)交互,该 API 使用 JSON 响应 GET "/api/v1/users?username=mcclure.rocio":
{
"user": {
"id": 5,
"name": "Rocio McClure",
"username": "mcclure.rocio",
"email": "rocio.mcclure@yahoo.com"
}
}
我的路由器是:
Router.map(function() {
this.route("login");
this.route("user", {path: "user/:username"}, function() {
this.route("profile");
});
});
所以我有类似http://localhost:4200/user/mcclure.rocio 这样的路线,这是一种用户的总结。
问题是在路由中加载了正确的模型:
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model: function(params) {
return this.store.find('user', { username: params.username })
}
});
我的 Ember 检查器指出加载的模型是一个空的 DS.AdapterPopulatedRecordArray。这是因为 findQuery(实际上是在我提供查询对象时调用)期望获取 JSON 数组,而我的 API 返回单个 user JSON 对象,因此它将其转换为空数组。
但是this.store.find('user', { username: params.username }) 为我的 API 构建了正确的请求,但我怎样才能让 Store 接受 API 响应并将其作为我的路由的模型?
注意: 如果我的 API 返回一个数组,则可以执行以下操作:
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model: function(params) {
return this.store.find('user', { username: params.username }).then(function(data){
return data.objectAtContent(0);
});
}
});
但是,我不想修改它。
【问题讨论】:
标签: ember.js ember-data