Andy Davison
Andy Davison

Reputation: 591

How to use ember-power-select with Ember-Data results

G'day,

I was trying to load model data from a store.findAll() call into the ember-power-select component. The data from findAll() loads into the component fine, but I needed to set the initial selected item, and nothing I did seemed to do the trick.

Upvotes: 0

Views: 611

Answers (2)

Keo
Keo

Reputation: 1153

Your solution will ask server twice. Check this solution (es6 syntax)

setupController(controller, model) {
  controller.set('model', model);
  this.store.findAll('club').then(clubs => {
    controller.set('clubs', clubs);
    // i am not sure if the function is called filter, but it should be close enough
    return clubs.filter(club => clubs.get('id') === model.get('clubId')).get('firstObject');
  }).then(selectedClub =>  controller.set('selectedClub', selectedClub)).catch(err => {
    console.log("Error:", err);
  });
}

Upvotes: 1

Andy Davison
Andy Davison

Reputation: 591

The trick was to use the get() method with 'firstObject' after you pull the element out of the DS cache with store.query.

Here's what I wound up doing - if anyone has a more elegant way of doing this I'd love to hear...

setupController: function(controller, model) {
  var _this = this;

  controller.set('model',model);

  _this.store.findAll('club')
  .then(function(results){
     controller.set('clubs', results);
     return _this.store.query('club', {id:model.get('clubId')});
  })
  .then(function(result){
     controller.set('selectedClub', result.get('firstObject'));
  })
  .catch(function(err){
    console.log("Error:",err);
  });

},

Upvotes: 0

Related Questions