Ember数据无法加载关系

时间:2014-03-03 02:48:22

标签: ember.js ember-app-kit

我在使用ember-data加载一些相关模型时遇到问题。我很确定这是我的语法错误,但我无法看到它。也许有人可能成为我的第二眼?

具体问题是我的模板中没有显示相关模型。

没有进一步的麻烦:

注释

  • 我正在使用ember-appkit-rails
  • 这是我的第一个余烬项目,所以请原谅我的n00bness。
  • 我觉得答案可能与嵌入式有关:'总是',但我不知道如何将它集成到我的代码中。
  • DEBUG:Ember:1.4.0 ember.js?body = 1:3462
  • 调查:Ember数据:1.0.0-beta.7 + canary.f482da04 ember.js?body = 1:3462
  • DEBUG:Handlebars:1.3.0 ember.js?body = 1:3462
  • DEBUG:jQuery:1.11.0 ember.js?body = 1:3462

有问题的模板

我认为这应该有效。它几乎就在ember-appkit-rails生成器之外。编剧和作曲家的名字没有出现。

<h3>{{id}}</h3>

<p>{{link-to 'Edit' 'operas.edit' this}} <button {{action destroyRecord this}}>Destroy</button></p>

<ul>
  <li>Title: {{title}}</li>
  <li>Alternate Title: {{alternate_title}}</li>
  <li>Source: {{source}}</li>
  <li>Librettist: {{librettist.name}}</li>
  <li>Composer: {{composer.name}}</li>
  <li>Notes: {{notes}}</li>
</ul>

模型

opera.js.es6

export default DS.Model.extend({
  title: DS.attr('string'),
  alternateTitle: DS.attr('string'),
  source: DS.attr('string'),
  librettistId: DS.attr('number'),
  composerId: DS.attr('number'),
  notes: DS.attr('string'),
  composer: DS.belongsTo('composer'),
  librettist: DS.belongsTo('librettist')
});

librettist.js.es6

export default DS.Model.extend({
  name: DS.attr('string'),
  operas: DS.hasMany('opera')
});

composer.js.es6

export default DS.Model.extend({
  name: DS.attr('string'),
  operas: DS.hasMany('opera')
});

ActiveModel Serializer

class OperaSerializer < ActiveModel::Serializer
  embed :ids, include: true

  attributes :id, :title, :alternate_title, :source, :librettist_id, :composer_id, :notes
  has_one :composer
  has_one :librettist
end

返回示例JSON

这是我在看一个歌剧唱片时看到的,但结构适用于整个集合:

{
  "composers": [
    {
      "id": 4097,
      "name": "Müller, Wenzel"
    }
  ],
  "librettists": [
    {
      "id": 1414,
      "name": "Bäuerle, Adolf"
    }
  ],
  "opera": {
    "alternate_title": "oder Wien in einem anderen Weltteile",
    "composer_id": 4097,
    "id": 4166,
    "librettist_id": 1414,
    "notes": "these are some notes",
    "source": "F116.Theater.a.d.Wien.260A.Mus; Mus.Hs.78.Mus; Mus.Hs.25409.Mus",
    "title": "Aline Königin von Golkonda"
  }
}

我也尝试过这种方法使用内联嵌入({opera: {...composer: {...}}}),但这种方法效果不佳。

感谢您的帮助! 保罗。

1 个答案:

答案 0 :(得分:0)

如果您使用_id作为客户端适配器,则不需要RESTAdapter

App.Opera = DS.Model.extend({
  title: DS.attr('string'),
  alternateTitle: DS.attr('string'),
  source: DS.attr('string'),
  notes: DS.attr('string'),
  composer: DS.belongsTo('composer'),
  librettist: DS.belongsTo('librettist')
});

App.Librettist = DS.Model.extend({
  name: DS.attr('string'),
  operas: DS.hasMany('opera')
});

App.Composer = DS.Model.extend({
  name: DS.attr('string'),
  operas: DS.hasMany('opera')
});

JSON

{
  "composers": [
    {
      "id": 4097,
      "name": "Müller, Wenzel"
    }
  ],
  "librettists": [
    {
      "id": 1414,
      "name": "Bäuerle, Adolf"
    }
  ],
  "opera": {
    "alternate_title": "oder Wien in einem anderen Weltteile",
    "composer": 4097,
    "id": 4166,
    "librettist": 1414,
    "notes": "these are some notes",
    "source": "F116.Theater.a.d.Wien.260A.Mus; Mus.Hs.78.Mus; Mus.Hs.25409.Mus",
    "title": "Aline Königin von Golkonda"
  }
}

http://emberjs.jsbin.com/OxIDiVU/233/edit

如果您使用的是ActiveModelAdapter,那么您将使用json中的_id,这是ActiveModelAdapter

的示例

http://emberjs.jsbin.com/OxIDiVU/234/edit