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

时间:2015-01-02 03:50:09

标签: javascript ember.js ember-data

我有后端服务于此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".

1 个答案:

答案 0 :(得分:1)

您使用了错误的Serializer

注释掉JSONSerializer并激活ActiveModelSerializer而你应该好好去

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

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

工作解决方案here