最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there an always callback for Ember.js .then function? - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question asked Sep 25, 2013 at 11:23 p1100ip1100i 3,7402 gold badges31 silver badges45 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

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()
});

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

Ember uses the RSVP.js library for promises, and RSVP does not support always due to not being part of the Promises/A(+) specs.

If you need it, @wycats suggests the following approach:

Ember.RSVP.Promise.prototype.andThen = function(success, error, always) {
  return this.then(function(value) {
    var ret = success(value);
    always(value);
    return ret;
  }, function(reason) {
    var ret = error(reason);
    always(reason);
    return ret;
  });
};

gorner's solution works but for Ember Data you have to add the following as well:

Ember.PromiseProxyMixin.reopen({
  andThen: function() {
    var promise = this.get('promise');
    return promise['andThen'].apply(promise, arguments);
  }
});

The reason is that the DS.Model.save() function returns a PromiseObject (see http://emberjs.com/api/data/classes/DS.PromiseObject.html), which doesn't implement Ember.RSVP.Promise but instead implements Ember.PromiseProxyMixin. So you have to make the andThen function available in that mixin in order for it to work with promises when saving models.

发布评论

评论列表(0)

  1. 暂无评论