[英]Ember-Data and pulling data into the store
我正在学习Ember,尝试从JSON(通过REST)将数据获取到Ember-Data存储中有点疯狂。
我已经这样扩展了适配器:
App.PostsAdapter = DS.RESTAdapter.extend({
host: "https://www.foobar.com",
namespace:"rest",
});
“ rest”是服务器上已设置为接受某些调用的目录(/ rest / posts发送所有帖子的JSON字符串,/ rest / posts / 1发送索引为1的帖子,依此类推)
我可以使用以下方法处理HTML中的路由:
App.Router.map(function() {
this.resource('about');
this.resource('posts', function() {
this.resource('post', { path: ':post_id' });
});
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('posts');
}
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
return this.store.findBy('posts', params.post_id);
}
});
这会在Chrome的Ember控制台插件中引发错误“找不到0的模型”,没有特定的代码行可以解决。 我已经尝试过可以在网上找到或想到的所有排列方式,但没有任何效果。 真是疯了。 当我指向代码中的数组时,一切正常,但是我无法终生弄清楚为什么AJAX调用失败。
JSON示例:
[{
"id": "27",
"title": "TITLE A",
"short_description": "BLAH BLAH",
"description": " MORE BLAH BLAH BLAH",
"keywords": "etc etc etc"
},
{
"id": "26",
"title": "TITLE B",
"short_description": "BLAH BLAH",
"description": " MORE BLAH BLAH BLAH",
"keywords": "etc etc etc"
}]
我肯定我很想念我,但是如果有人能指出正确的方向,我将非常感谢!
返回的JSON需要包含相应模型的名称。
此处的文档中对此进行了介绍: http : //emberjs.com/guides/models/the-rest-adapter/#toc_json-root
因此,您需要返回:
{
"posts": [
{
"id": "27",
"title": "TITLE A",
"short_description": "BLAH BLAH",
"description": " MORE BLAH BLAH BLAH",
"keywords": "etc etc etc"
},
{
"id": "26",
"title": "TITLE B",
"short_description": "BLAH BLAH",
"description": " MORE BLAH BLAH BLAH",
"keywords": "etc etc etc"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.