在EmberJS中返回记录和其他模型

时间:2016-08-11 22:38:44

标签: ember.js ember-model

使用以下代码,您可以返回多个模型(假设您之前已经提取过您的数据):

model: function() {

    return Ember.RSVP.hash({
        cars: this.store.peekAll('car'),
        owners: this.store.peekAll('owner'),
        parts: this.store.peekAll('part')
    });

}

然后你可以显示它们:

{{#each model.cars as |car|}}
    {{car.year}}<br>
{{/each}}

等等。

查找特定的汽车记录非常简单:

model: function(params) {
    return this.store.peekRecord('car',params.car_id);
}

如果在所有业主加载到页面的相同时间内,如何找到特定的汽车记录?

当你点击这个网址时,这样的东西似乎不起作用: / car / 1

model: function(params) {

    return Ember.RSVP.hash({
        cars: this.store.peekRecord('car',params.car_id),
        owners: this.store.peekAll('owner'),
        parts: this.store.peekAll('part')
    });

}

您可以争辩使用belongsTo或hasMany ......但是部件或所有者尚未分配到汽车记录,只需在页面上使用它们,因此用户可以将多个所有者或部件分配给一辆汽车决定这样做。

1 个答案:

答案 0 :(得分:-1)

我最近刚遇到类似的问题。

我最终得到了这个:

model(params) {
    return this.get('store').findRecord('book',params.book_id, {include: 'author, tags'});
  },
  afterModel() {
    var self = this;
    return Ember.RSVP.hash({
      tags: this.get('store').findAll('tag'),
      authors: this.get('store').findAll('author')
    }).then(function(result) {
      self.set('tags', result.tags);
      self.set('authors', result.authors);
    });
  },
  setupController(controller, model) {
    this._super(controller, model);
    controller.set('tags', this.get('tags'));
    controller.set('authors', this.get('authors'));
  },

对于包含id的路由(例如:/ books / 1 / edit),模型必须包含路由本身的对象,就像路由时一样,模型由&#34; ember-data&#34;自动填充。从其缓存(没有http请求)。