【发布时间】:2014-07-25 16:16:43
【问题描述】:
我有一个现成的 Django 项目,它通过 rest_framework 及其视图集提供 JSON。现在我想用 Ember 编写客户端。这是我的设置:
- Django 1.6.5
- 灰烬 1.6.1
- Ember-Data 1.0.0-beta.8.2a68c63a
- jQuery 2.1.1
我的 Django 服务器在 localhost 下的默认端口 8000 上运行。我通过在浏览器中打开 index.html 来测试我的 Ember 应用程序。因此,我像这样自定义了 ApplicationAdapter
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:8000',
});
我尝试从http://localhost:8000/artists 获取艺术家列表。指定路线是
App.ArtistsRoute = Ember.Route.extend({
model: function() {
this.store.find('artists');
}
});
当我在浏览器中打开上述网址时,我从服务器返回的响应是
[
{
"id": 1,
"name": "Kollegah",
"origin": "Germany",
"genre": "German Rap"
},
{
"id": 2,
"name": "Peter Fox",
"origin": "Germany",
"genre": "Hip-Hop"
},
{
"id": 3,
"name": "Farid Bang",
"origin": "Germany",
"genre": "German Rap"
},
{
"id": 4,
"name": "Eko Fresh",
"origin": "Germany",
"genre": "German Rap"
}
]
获取数据时出现以下两个 Ember 错误:
-Error while processing route: artists No model was found for 'artists' Error: No model was found for 'artists'
-No model was found for 'artists' Error: No model was found for 'artists'
问题是我已经指定了模型
var attr = DS.attr;
App.Artist = DS.Model.extend({
name: attr,
origin: attr,
genre: attr,
});
我想问题是每个 JSON 响应开头缺少根元素。我建议它应该看起来像 Ember Guides 中的这个例子:
{
"post": {
"id": 1,
"title": "Rails is omakase",
"comments": ["1", "2"],
"user" : "dhh"
},
"comments": [{
"id": "1",
"body": "Rails is unagi"
}, {
"id": "2",
"body": "Omakase O_o"
}]
}
搜索了一小会后,我发现Rails 存在类似的问题。我尝试了another Stackoverflow question 中提到的 Django 解决方案,但我得到了同样的错误。
有人知道这个问题的服务器端解决方案吗? Ember Data Django Adapter 可能是客户端之一。不幸的是,它被设计为一个节点插件,目前我没有在我的项目中使用它。
【问题讨论】:
标签: json django rest ember.js django-rest-framework