简体   繁体   English

使用Ember.PromiseProxyMixin时如何冒泡被拒绝的承诺

[英]How to bubble up a rejected promise when using Ember.PromiseProxyMixin

I am using Ember's PromiseProxyMixin with AJAX data calls and Ember RSVP Promises. 我正在使用Ember的PromiseProxyMixin和AJAX数据调用以及Ember RSVP Promises。 Rather than incorporating error-handling in each route/template, I would like to bubble a rejected promise up to an error handler in the Application route as follows: 我不想在每个路由/模板中加入错误处理,而是将拒绝的承诺冒充到Application路由中的错误处理程序,如下所示:

export default Ember.Route.extend({
    actions: {
        error: function(error, transition) {
            return this.transitionTo('error');
            return false;
        }
    }
});

Currently, if a promise is rejected, the rejected promise doesn't appear bubble up to the Application route (is this because the PromiseProxyMixin attaches to a promise's .fail() function and prevents further bubbling? If so, is there any way of continuing the bubbling?) 目前,如果承诺被拒绝,被拒绝的承诺似乎没有出现在Application路由中(这是因为PromiseProxyMixin附加到promise的.fail()函数并阻止进一步冒泡?如果是这样,有没有办法继续冒泡?)

Is it possible to use the PromiseProxyMixin and also allow the rejected promise to bubble up to the Application route? 是否可以使用PromiseProxyMixin并允许被拒绝的承诺冒泡到Application路由?

I'm not sure that it will solve your problem, but we did encounter differences in Es6 promises and jQuery promises, therefore we convert all jQuery promises to Es6 by default using the following initializer. 我不确定它会解决你的问题,但我们确实遇到了Es6 promises和jQuery promises的差异,因此我们默认使用以下初始化程序将所有jQuery promise转换为Es6。 We also convert other "thennables" using the when method below: 我们还使用下面的when方法转换其他“thennables”:


import Ember from 'ember';

function initialize() {
  var $ajax = Ember.$.ajax;
  Ember.RSVP.when = function(promise, label) {
    return new Ember.RSVP.Promise(promise.then.bind(promise), label);
  };
  return Ember.$.ajax = function() {
    return Ember.RSVP.when($ajax.apply(Ember.$, arguments), '$.ajax');
  };
};

var PromiseAdapterInitializer = {
  name: 'promise-adapter',
  initialize: initialize
};

export {initialize};
export default PromiseAdapterInitializer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM