如何正确执行ember运行循环中的Ember.RSVP.all

Tor*_*ups 4 ember.js rsvp.js

我正试图在Ember.RSVP.all中执行一个承诺

App.Foo = Ember.Object.create({
    bar: function() {
        var configuration = ajaxPromise("/api/configuration/", "GET");
        Ember.RSVP.all([configuration]).then(function(response) {
            //do something with the response in here
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

但是因为我的集成测试模拟xhr w/out一个运行循环,测试失败并出现预期错误"你已经打开测试模式,这会禁用运行循环'自动运行"

所以我用一个简单的ember.run包装了RSVP

App.Foo = Ember.Object.create({
    bar: function() {
        var configuration = ajaxPromise("/api/configuration/", "GET");
        Ember.run(function() {
            Ember.RSVP.all([configuration]).then(function(response) {
                //do something with the response in here
            });
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

但我仍然因为一些奇怪的原因而得到错误.注意 - 如果我稍后运行它没关系(这不会工作,因为我需要执行异步代码以使此测试正常工作)

App.Foo = Ember.Object.create({
    bar: function() {
        var configuration = ajaxPromise("/api/configuration/", "GET");
        Ember.run.later(function() {
            Ember.RSVP.all([configuration]).then(function(response) {
                //do something with the response in here
            });
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

这是我的ajaxPromise实现-fyi

var ajaxPromise = function(url, type, hash) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
        hash = hash || {};
        hash.url = url;
        hash.type = type;
        hash.dataType = 'json';

        hash.success = function(json) {
            Ember.run(null, resolve, json);
        };

        hash.error = function(json) {
            Ember.run(null, reject, json);
        };

        $.ajax(hash);
    });
}
Run Code Online (Sandbox Code Playgroud)

如何将Ember.RVSP包裹在我的余烬运行中,并抛出此错误?

更新

这是我的测试设置(包括我的助手)

document.write('<div id="ember-testing-container"><div id="wrap"></div></div>');
App.setupForTesting();
App.injectTestHelpers();

test("test this async stuff works", function() {
    visit("/").then(function() {
        equal(1, 1, "omg");
    });
});
Run Code Online (Sandbox Code Playgroud)

我遗漏的唯一部分是我正在使用jquery-mockjax,所以没有运行循环包装xhr模拟(部分原因是我喜欢这个库,当我不用异步代码包装时,它会失败测试核心团队建议运行循环)

Ste*_*ner 7

这可能与您的测试运行方式有关,因此如果您可以提供测试,那将会很有帮助

我也注意到:

事实证明,我相信你也正在(或将很快)被jQuery的jQXHR对象所控制,这是一个错误的承诺,由于0原因实现了自己,并强制执行自己的nextTurn.这导致自动运行.这只会发生在错误场景中.

在ember数据中,我们通过剥离thenjQXHR对象来对其进行排序

请参阅:https: //github.com/emberjs/data/blob/4bca3d7e86043c7c5c4a854052a99dc2b4089be7/packages/ember-data/lib/adapters/rest_adapter.js#L539-L541

我怀疑以下内容将清除这一点.

var ajaxPromise = function(url, type, hash) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
        hash = hash || {};
        hash.url = url;
        hash.type = type;
        hash.dataType = 'json';

        hash.success = function(json) {
            Ember.run(null, resolve, json);
        };

        hash.error = function(json) {
            if (json && json.then) { json.then = null } // this line

            Ember.run(null, reject, json);
        };

        $.ajax(hash);
    });
}
Run Code Online (Sandbox Code Playgroud)

这是相当不幸的,各种各样的概念和想法汇集在一起​​,给你带来痛苦.我们希望(很快)土地Ember.ajax将所有这些疯狂化.

也可以随时查看ember-data的内容:https://github.com/emberjs/data/blob/4bca3d7e86043c7c5c4a854052a99dc2b4089be7/packages/ember-data/lib/adapters/rest_adapter.js#L570-L586