backbone-deferred icon indicating copy to clipboard operation
backbone-deferred copied to clipboard

jqXHR objects already implement Promise interface

Open neverfox opened this issue 11 years ago • 5 comments

I think I must be missing something here, but Backbone already implicitly implements the Promise interface via jQuery. "The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise." You can already write things like myCollection.fetch().then(...) without any special library. What does this library add?

neverfox avatar Mar 07 '14 04:03 neverfox

@neverfox Thanks for your concern and this probably does need to be explained better in the docs. What Backbone returns in its callbacks is no the same as in the jqXHR object. This library allows you to use the Backbone syntax in your callbacks just using deferreds rather than passing callbacks. While you can accomplish a lot of the same goals using the jQuery methods it isn't as intuative and doesn't allow quick reference to the Backbone docs. This library looks to just apply some of that syntactic sugar.

arhea avatar Mar 13 '14 19:03 arhea

@arhea I don't understand it neither, Backbone returns a real jqXHR object (which implements promise interface, as @neverfox states), and what more syntactic sugar exists besides this?

 var myModel = new MyModel({ some: 'data' });
 myModel.save()
    .then(function () { // Success callback })
    .fail(function () { // Failure callback })
    .always(function () { // Always fired callback });

piflex avatar Nov 07 '14 07:11 piflex

The big difference is that in the success callback of Backbone (when passed in a hash), you're passed as arguments some important things such as model, response, options (if it were a model).

If using the jqXHR promises, you get the normal jqXHR arguments, such as the decoded response, the human readable status and the jqXHR. This add-on, as far as I can tell, lets you use the promise API, but receive the same style arguments had you used the success property.

alexanderdickson avatar Nov 12 '14 00:11 alexanderdickson

@alexanderdickson is right. @piflex I can do a better job of explaining in the docs. The idea is the Promise sugar with the Backbone API. It also, ensures that all the Backbone events complete before you start reacting to the response.

arhea avatar Nov 19 '14 23:11 arhea

I see. Thanks!

neverfox avatar Dec 04 '14 00:12 neverfox