Ember.js REST Ajax成功和错误 [英] Ember.js REST Ajax Success and Error

查看:344
本文介绍了Ember.js REST Ajax成功和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Ember.js RESTAdapter的成功和错误 =https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember-data/lib/adapters/rest_adapter.js#L341-L347 =nofollow> ajax功能

I'd like to know what the success and error do in the Ember.js RESTAdapter's ajax function.

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

hash.error = function(jqXHR, textStatus, errorThrown) {
  Ember.run(null, reject, jqXHR);
};

我知道哈希通过AJAX发送的数据,但是成功错误扮演什么角色?我假设他们将基于成功或错误的AJAX响应运行,对吧?它们在AJAX被调用之前被设置为回调?他们如何工作?

I know hash is the data sent through AJAX, but what role do success and error play? I assume they'd be run based on a successful or erroneous AJAX response, right? They're set before the AJAX is called, as callbacks? How do they work?

推荐答案


但成功和错误的作用是什么?我假设他们将基于成功或错误的AJAX响应运行,对吗?

but what role do success and error play? I assume they'd be run based on a successful or erroneous AJAX response, right?

对,因为ember使用jQuery所提到的功能只是简单的jQuery方法。

Right, since ember uses jQuery under the hood the functions mentioned are just plain jQuery methods.


它们在AJAX被调用之前设置为回调?他们如何工作?

They're set before the AJAX is called, as callbacks? How do they work?

关于函数本身,请参阅从 jQuery官方文档

As for the functions itself, see this info taken from the jQuery official docs:


  • 如果请求失败,则会调用错误回调选项。它接收到jqXHR,一个指示错误类型的字符串,以及一个异常对象(如果适用)。一些内置错误将提供一个字符串作为异常对象:abort,timeout,No Transport。

  • error callback option is invoked, if the request fails. It receives the jqXHR, a string indicating the error type, and an exception object if applicable. Some built-in errors will provide a string as the exception object: "abort", "timeout", "No Transport".

/ strong>回调选项被调用,如果请求成功。它收到返回的数据,包含成功代码的字符串和jqXHR对象。

success callback option is invoked, if the request succeeds. It receives the returned data, a string containing the success code, and the jqXHR object.

我还应该提到成功回调是在最近的jQuery版本被替换为完成,并被标记为已弃用,如下所述:

I should also mention that the success callback is in recently jQuery version being replaced with done and is marked as deprecated as noted here:


弃用注意事项:从jQuery 1.8开始,不推荐使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。要准备好最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

但是不要担心,因为我猜到,jQuery完全删除这个方法,ember团队一定会赶上新的回调版本。

But don't worry, because I guess until jQuery removes this methods completely the ember team has surely catched up with the new callback versions.

最后如果你在哪里想知道对 Ember.run 的调用是否可以看看这里。但是基本上它确保传递的目标和方法在RunLoop内运行,确保还有任何延迟的动作,如绑定和视图更新,最后会刷新。 Runloop 也非常翔实。

And finally if you where wondering what the call to Ember.run does you can have a look here. But basically it ensures that the passed target and method are run inside of a RunLoop, ensuring also any deferred actions like bindings and views updates, this are flushed at the end. This SO answer on the Runloop is also very informative.

希望有帮助。

这篇关于Ember.js REST Ajax成功和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆