Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an always callback for Ember.js .then function?

Assume I got an Ember obj. When doing any kind of sync with backend there is a possiblity to use a promise chain:

obj.save().then(function(res){
  // Success callback
}, function(res){
  // Fail callback
});

Is there a done/always callback for Ember.js promise chain with .then()?

I've tried adding a third parameter function, but it did not help.

like image 605
p1100i Avatar asked Sep 25 '13 11:09

p1100i


People also ask

How do I resolve a promise in Ember?

let promise = new Promise(function(resolve, reject) { // on success resolve(value); // on failure reject(reason); }); promise. then(function(value) { // on fulfillment }, function(reason) { // on rejection });

How does Ember JS work?

Ember uses templates to organize the layout of HTML in an application. Ember templates use the syntax of Handlebars templates. Anything that is valid Handlebars syntax is valid Ember syntax. Here, {{name}} is a property provided by the template's context.


2 Answers

http://emberjs.com/api/classes/Ember.PromiseProxyMixin.html#method_finally

Ember -> jQuery

  1. .then() -> .done()
  2. .catch() -> .fail()
  3. .finally() -> .always()

Example (in the router):

var self = this;
var modelType = this.store.createRecord('modelType', {/* model attrs */});

modelType.save().then(function(model){
  self.transitionTo('model.show', model);
}).catch(function(){
  console.log('Failure to Save: ', reason);
}).finally({
  self.hideSpinner()
});
like image 169
Taysky Avatar answered Oct 17 '22 14:10

Taysky


Unfortunately there isn't. But you can create your own modifying the RSVP.Promise prototype:

Ember.RSVP.Promise.prototype.always = function(func) {
  return this.then(func, func);
}

So you can do the following:

// will show success
Ember.RSVP.resolve('success').always(function(msg) { 
  alert(msg) 
})

// will show error
Ember.RSVP.reject('error').always(function(msg) { 
  alert(msg) 
})

I hope it helps

like image 3
Marcio Junior Avatar answered Oct 17 '22 13:10

Marcio Junior