簡體   English   中英

為什么我的記錄空了?

[英]Why I'm getting an empty store of records?

我有為這個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'
});

這里的工作解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM