Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an Ember.js controller variable to an Ember Data object, not a promise

I have a route that initially has no suggestion. Based on an action, I would like to grab a suggestions array with Ember Data, get the first suggestion and assign it to the controller. Here's what I've got:

App.IndexRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    this._super(controller, model);
    controller.set('suggestion', null);
  },
  actions: {
    getSuggestion: function() {
      suggestion = this.store.find('suggestion').then(function(s) {
          return s.get('firstObject');
      });
      this.controller.set('suggestion', suggestion);
    }
  }
});

The problem is that the suggestion variable, after performing the getSuggestion action, it still a promise. How can I only set the controller variable after the promise is resolved? Or how can I let it resolve afterwards and have the variable updated with the actual object?

like image 726
linkyndy Avatar asked Jan 01 '26 01:01

linkyndy


1 Answers

Set the property on resolution of the promise:

actions: {
    getSuggestion: function() {
        var self = this;
        this.store.find('suggestion').then(function(s) {
            self.controller.set('suggestion', s.get('firstObject'));
        });
    }
}
like image 153
mistahenry Avatar answered Jan 03 '26 14:01

mistahenry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!