简体   繁体   English

灰烬数据并将数据拉入存储

[英]Ember-Data and pulling data into the store

I'm learning Ember and going a bit insane trying to get data from JSON (via REST) into the Ember-Data store. 我正在学习Ember,尝试从JSON(通过REST)将数据获取到Ember-Data存储中有点疯狂。

I've extended an adapter as such: 我已经这样扩展了适配器:

 App.PostsAdapter = DS.RESTAdapter.extend({
   host: "https://www.foobar.com",
   namespace:"rest",
 }); 

"rest" is a directory on my server already setup to accept certain calls ( /rest/posts sends a JSON string of all posts, /rest/posts/1 sends the post with index 1, etc.) “ rest”是服务器上已设置为接受某些调用的目录(/ rest / posts发送所有帖子的JSON字符串,/ rest / posts / 1发送索引为1的帖子,依此类推)

I have the following to handle the routes in my HTML: 我可以使用以下方法处理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);
}
});

This is throwing an error "No model was found for 0" in the Ember console plugin for Chrome, with no specific line of code to work off of. 这会在Chrome的Ember控制台插件中引发错误“找不到0的模型”,没有特定的代码行可以解决。 I've tried every permutation of this that I can think of or find online, and nothing's working. 我已经尝试过可以在网上找到或想到的所有排列方式,但没有任何效果。 Really going insane. 真是疯了。 When I point to an array in the code, everything works, but I can't for the life of me figure out why the AJAX call is failing. 当我指向代码中的数组时,一切正常,但是我无法终生弄清楚为什么AJAX调用失败。

Example JSON : 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"
}]

I'm sure it's something super obvious that I'm missing, but if anyone can point me in the right direction, I'd be super grateful!! 我肯定我很想念我,但是如果有人能指出正确的方向,我将非常感谢!

The returned JSON needs to include the name of the corresponding model. 返回的JSON需要包含相应模型的名称。

This is covered in the docs here: http://emberjs.com/guides/models/the-rest-adapter/#toc_json-root 此处的文档中对此进行了介绍: http : //emberjs.com/guides/models/the-rest-adapter/#toc_json-root

So you need to return: 因此,您需要返回:

{
    "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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM