Ember - How do you select elements in multiselect when using the hasMany attribute?

I am currently having problems selecting items in multi-segment view. I am using ember 1.3 with ember-data 1.0.0 beta 6 and ember-data-django-rest-adapter.

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()
});

Template:

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

I'm not sure why this does not work, because when I output allAuthors and authors using #each in the template, I get the data I need.

Is there something I am missing?

Thanks in advance for your help.

+4
source share
1 answer

I usually pre-fill such data on the route using the promise:

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: []
});

, , , .

0

Source: https://habr.com/ru/post/1523287/


All Articles