Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
If my answer didn't satisfy your question it would be nice to know what's unclear so I can improve it further. –  intuitivepixel Jul 11 '13 at 15:11
    
Hi intuitivepixel, I'm not skilled enough yet to fully comprehend the answer. I need to read up on the jQuery docs to avoid asking trivial questions. Thanks for the follow-up! I guess I can always comment on it if I have further questions. –  HaoQi Li Jul 11 '13 at 15:25
    
Hi HaoQi Li, yes of course, comment as you like, and by the way, thanks for accepting the answer. –  intuitivepixel Jul 11 '13 at 15:35
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
1  
great answer, didn't realize those callbacks were going away... –  Mike Grassotti Jul 10 '13 at 2:56
    
thanks @MikeGrassotti :) I already thought of proposing a PR to update to done and fail, but I'm somewhat concerned to introduce breaking changes by doing this... –  intuitivepixel Jul 10 '13 at 11:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.