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

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

问题描述

我想知道是什么成功错误做的Ember.js RESTAdapter的<一个href="https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember-data/lib/adapters/rest_adapter.js#L341-L347"相对=nofollow> AJAX功能。

  hash.success =功能(JSON){
    Ember.run(空,解决,JSON);
  };

  hash.error =功能(jqXHR,textStatus,errorThrown){
    Ember.run(空,拒绝,jqXHR);
  };
 

我知道是<一个href="https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember-data/lib/adapters/rest_adapter.js#L349"相对=nofollow>通过AJAX 发送的数据,但什么样的作用成功错误玩?我认为他们会根据一个成功或错误的Ajax响应运行,对吧?他们的AJAX调用之前正在设置,如回调?它们是如何工作的?

解决方案
  

但什么作用都成功和错误玩吗?我认为他们会根据一个成功或错误的Ajax响应运行,对吧?

权利,因为烬使用jQuery的引擎盖下提到的功能都只是普通的jQuery方法。

  

他们是AJAX调用之前设置,如回调?它们是如何工作的?

对于功能本身,看到这个信息从 jQuery的官方文档:

  • 错误回调选项调用,如果请求失败。它接收jqXHR,一个字符串,指示错误类型,以及一个异常对象(如果适用)。一些内置的错误会提供一个字符串作为异常对象:中止,暂停,无交通运输

  • 成功回调选项调用,如果请求成功。它接收返回的数据,包含成功code字符串,以及jqXHR对象。

我还要提到的是,成功回调是近期的jQuery版本替换为完成并标记为pcated作为德$ P $这里要注意:

  

德precation通知:jqXHR.success(),jqXHR.error(),和jqXHR.complete()回调pcated在jQuery 1.8代$ P $。为prepare您code为他们的最终消除,使用jqXHR.done(),jqXHR.fail(),和jqXHR.always()来代替。

不过不要担心,因为我想,直到jQuery的移除此方法完全余烬队肯定已经逮住了新的回调版本。

最后,如果你在那里想知道什么是呼叫 Ember.run 也可以看看的此处。但基本上它确保传递的目标和方法一RunLoop内运行,确保还喜欢绑定和观点更新的任何延期的行动,这是在冲洗结束。 这SO回答上的 Runloop 也是非常丰富的。

希望它帮助。

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

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?

解决方案

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

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

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

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

  • 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".

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

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:

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.

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.

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.

Hope it helps.

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

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