Ember js使用HATEOAS REST API

时间:2014-06-19 04:18:38

标签: ember.js spring-hateoas

我有一个REST端点"http://localhost:8080/customers",我想挂钩到我的Ember JS应用程序。

App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.extend({
url: 'http://localhost:8080/customers'
})
});

REST Payload

{
  "_links" : {
    "search" : {
      "href" : "http://localhost:8080/customers/search"
    }
  },
  "_embedded" : {
    "customers" : [ {
      "id" : 2,
      "firstName" : "Jim",
      "lastName" : "Smith",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/customers/2"
        }
      }
    }, {
      "id" : 1,
      "firstName" : "Jimmy",
      "lastName" : "Jones",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/customers/1"
        }
      }
    } ]
  }
}

我继续收到此错误。这是我在Ember JS的第二周。说实话,这比我想象的要困难得多。不太容易调试或理解。

加载路线时出错:

TypeError: undefined is not a function
at App.CustomersRoute.Ember.Route.extend.model

1 个答案:

答案 0 :(得分:0)

基本上,您需要使用自定义序列化程序将数据转换为此格式:

{
  "customers" : [ 
    {
      "id" : 2,
      "firstName" : "Jim",
      "lastName" : "Smith"
    }, {
      "id" : 1,
      "firstName" : "Jimmy",
      "lastName" : "Jones"
    } 
  ]
}

您需要阅读https://github.com/emberjs/data/blob/master/TRANSITION.md,其中介绍了如何使用extractSingleextractArraynormalizenormalizeHash

你是一个特殊的例子,看起来接近api的例子: http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray

App.CustomerSerializer = DS.ActiveModelSerializer.extend({
  extractArray: function(store, type, payload) {
    // do your own implementation and return that, or modify it slightly
    // and do the default implementation
    return this._super(store, type, payload);
  },
});