灰烬 - 使用hasMany属性时,如何在多选中预选项目?

时间:2014-01-23 09:30:34

标签: ember.js ember-data

我目前在多选视图中预选项目时遇到问题。我正在使用带有ember-data 1.0.0 beta 6和ember-data-django-rest-adapter的ember 1.3。

App.Article = DS.Model.extend({
  title: attr(),
  description: attr(),
  authors: hasMany('author')
});

App.ArticleRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('article', params.article_id);
  },
  setupController: function(controller, model) {
    controller.set('content', model);
  }
});

App.ArticleController = Ember.ObjectController.extend({
  needs: ['authors'],
  allAuthors: function() {
    return this.store.find('author');
  }.property()
});

模板:

{{input authors as='select'
  multiple='true'
  collection='allAuthors'
  selection='authors'
  optionValuePath='content.id'
  optionLabelPath='content.name'}}

我不确定为什么这不起作用,因为当我在模板中使用 #each 输出 allAuthors 作者时,我'得到我应该的数据。

有什么我不想做的事吗?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我通常使用承诺在路线中预填充此类数据:

App.ArticleRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('article', params.article_id);
  },
  setupController: function(controller, model) {
    this.store.find('author').then(function(authors) {
      controller.set('allAuthors', authors);
      // or maybe controller.get('allAuthors').addObjects(authors);
    });
    controller.set('content', model);
  }
});

App.ArticleController = Ember.ObjectController.extend({
  needs: ['authors'],
  allAuthors: []
});

不确定这是否是最好的方法,但这对我有用。