为什么我得到一个空的记录存储

Why I'm getting an empty store of records?

本文关键字:一个 记录 存储 为什么      更新时间:2023-09-26

我有后端服务于这个json

{
  "bars": [
    {
      "id": 1,
      "name": "one bar"
    },
    {
      "id": 2,
      "name": "second bar"
    },
    {
      "id": 3,
      "name": "third bar"
    }
  ]
}

host: 'http://localhost:3000/bars'

这就是我在客户端拥有的全部内容:

;(function(Ember, DS) {
    var App = Ember.Application.create({
        LOG_TRANSITIONS: true
    });
    window.App= App;
    App.ApplicationAdapter = DS.RESTAdapter.extend({
        host: 'http://localhost:3000'
    });
    App.ApplicationSerializer = DS.JSONSerializer.extend({
        primaryKey: 'id'
    });
    App.Bar = DS.Model.extend({
        name: DS.attr('string')
    });

    App.ApplicationRoute = Ember.Route.extend({
        model: function() {
            var bars = this.store.find('bar');
            bars.then(function(bars) {
                console.log(bars.content.length);  // return 0 where the xhr request getting 3 items
        });
        return bars;
    }
    });
}(Ember, DS));

我正在使用

Ember      : 1.9.0
Ember Data : 1.0.0-beta.14.1
Handlebars : 2.0.0 
jQuery     : 2.1.1

PS:jquery ajax 请求似乎工作正常:

 XHR finished loading: GET "http://localhost:3000/bars".

您使用了错误的Serializer

注释掉JSONSerializer并激活ActiveModelSerializer,您应该很高兴

//     App.ApplicationSerializer = DS.JSONSerializer.extend({
//         primaryKey: 'id'
//     });
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
    primaryKey: 'id'
});

工作解决方案在这里