与非标准API的Ember数据关系

时间:2014-11-04 23:00:57

标签: ember.js ember-data

我想使用Ember Data,但我编写的API不符合所有惯例。

我的问题是API不会返回侧载关系,但应使用REST结构加载关系:/model/id/relationship

我有以下型号:

Library = DS.Model.extend
  name: DS.attr 'string'
  groups: DS.hasMany 'group', {async: true}
  groupsCount: DS.attr 'number'

有效载荷如下:

{
  library: {
    groups_count: 44,
    name: "demo",
    id: "545262a063726d2514390100"
  }
}

当我尝试使用library.get('groups')加载群组时,没有任何反应。它应该拨打libraries/545262a063726d2514390100/groups

我可以更改RESTAdapter以使其正常工作吗?

1 个答案:

答案 0 :(得分:0)

经过一番研究后,我发现了一种对我有用的方法:

LibrarySerializer = DS.RESTSerializer.extend
  normalize: (type, hash, prop)->
    hash.links =
      groups: "groups"
    @_super(type, hash, prop)

这实际上会在响应中添加一个links对象,使得其余适配器遵循该路径来检索关系。