我有一个准备好的Django项目,它通过rest_framework及其视图集提供JSON。现在我想使用Ember编写客户端。这是我的设置:
我的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');
}
});
当我在浏览器中打开提到的URL时,我从服务器返回的响应是
[
{
"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可以是客户端的一个。不幸的是,它被设计为节点插件,目前我还没有将它用于我的项目。
答案 0 :(得分:0)
Ember Data Django适配器是解决问题的最简单方法,如果您关注文档,则有instructions for using it without ember-cli。