Reputation: 17900
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?
Upvotes: 3
Views: 5406
Reputation: 1298
Could you do something like this with RSVP?
App.IndexRoute = Ember.Route.extend({
setupController: function(controller, model) {
this._super(controller, model);
controller.set('suggestion', null);
},
actions: {
getSuggestion: function() {
var suggestions = this.store.find('suggestion');
Ember.RSVP.all(suggestions).then(function(s) {
this.controller.set('suggestion', s.get('firstObject'));
}.bind(this));
}
}
});
Upvotes: 0
Reputation: 3075
You can change controller variable between controllers,
If you want to change controller variable of "home" controller then you need to include Home controller into your controller.
Example:-
export default Ember.Controller.extend({
needs: ['home'],
changeVariable: function(){
if(..){
this.set('controllers.home.isMyHomePage', false);
}else{
this.set('controllers.home.isMyHomePage', true);
}
}
});
Upvotes: 1
Reputation: 8724
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'));
});
}
}
Upvotes: 2
Reputation: 3121
You should set the 'suggestion' inside 'then' block
App.IndexRoute = Ember.Route.extend({
setupController: function(controller, model) {
this._super(controller, model);
controller.set('suggestion', null);
},
actions: {
getSuggestion: function() {
controller = this.controller;
this.store.find('suggestion').then(function(s) {
suggestion = s.get('firstObject');
controller.set('suggestion', suggestion);
});
}
}
});
Upvotes: 1