Ember.js REST Ajax Success and Error

匆匆过客 提交于 2019-12-10 20:18:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17559773/ember-js-rest-ajax-success-and-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!