sách gpt4 ai đã đi

ember.js - 如何在 ember 运行循环中正确执行 Ember.RSVP.all

In lại 作者:行者123 更新时间:2023-12-02 09:12:16 33 4
mua khóa gpt4 Nike

我正在尝试在 Ember.RSVP.all 内执行 promise

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

但是因为我的集成测试模拟了没有运行循环的 xhr,所以测试失败并出现预期错误“您已打开测试模式,这禁用了运行循环的自动运行”

所以我用一个简单的 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
});
});
}
});

但由于某些奇怪的原因我仍然收到错误。注意 - 如果我稍后运行就可以了(但这不起作用,因为我需要执行异步代码才能使此测试正常工作)

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

这是我的 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);
});
}

如何将 Ember.RVSP 包装在我的 ember 运行中而不抛出此错误?

làm mới

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

document.write('
');
App.setupForTesting();
App.injectTestHelpers();

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

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

1 Câu trả lời

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

我还注意到:

事实证明,我相信你也被(或很快)被 jQuery 的 jQXHR 对象所困扰,它是一个格式错误的 promise ,它以 0 理由实现自身,并强制它自己的 nextTurn 攻击你。这是导致自动运行的原因。这只会发生在错误场景中。

在 ember 数据中,我们通过从 jQXHR 对象中剥离 sau đó 来解决这个问题

参见: 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);
});
}

这是相当不幸的,各种独立的概念和想法聚集在一起会给你带来痛苦。我们希望(很快)能够实现 Ember.ajax,让所有这些疯狂的事情正常化。

也请随时查看 ember-data 的进展情况:https://github.com/emberjs/data/blob/4bca3d7e86043c7c5c4a854052a99dc2b4089be7/packages/ember-data/lib/adapters/rest_adapter.js#L570-L586

关于ember.js - 如何在 ember 运行循环中正确执行 Ember.RSVP.all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21241913/

33 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com