lundi 23 mars 2015

set controller attribute in promise before continue [ember cli]

Hi I have a doubt in how using promises in Ember. Basically I want to setup the attributes of my controller before running what is next the promise, my code:



export default Ember.Controller.extend({
min: null,
max: null,

isValid: Ember.computed(function(){
var self = this;

var result = new Ember.RSVP.Promise(function(resolve, reject){
var url = "..."
Ember.$.getJSON(url).then(function(data){
if (data[0] === 0){
self.set('errorMessage', "It is a free book");
return false;
} else if (data[0] > 2000){
self.set('errorMessage', "You are poor, go for next");
return false;
} else {
console.log(data); # line 2 of console result
self.set('min', data[1]);
self.set('max', data[2]);
return true;
}
});
});
}
return result;
}),

actions: {
save: function(){
if (this.get('isValid')){
console.log('save action is fired. -- VALID' + '--' + this.get('maxPrice')); # line 1 of console result
} else {
console.log('save action is fired. -- INVALID');
}
}
}

});


This thing is when in the third if statement the code in save action is running before setting the attributes min and max inside the promise. Result in console:



> save action is fired. -- VALID--null
> [9, 1, 20]


Any idea how to set the values before the action go ahead?


Thanks,





Aucun commentaire:

Enregistrer un commentaire