Skip to content

jQuery Plugin: Wrap jqXHR promise callbacks #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

matghaleb
Copy link

Since jQuery 1.5, jQuery.ajax implements the Promise interface and that's why the jqXHR.done(), jqXHR.fail(), jqXHR.always(), jqXHR.then() callbacks need to be wrapped too.
Note that the jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8 but are still exposed.

// call _oldAjax and store jqXHR
jqXHR = _oldAjax.call(this, url, options);
// wrap jqXHR promise handlers
wrapObjectKeys(jqXHR, ['done', 'success', 'fail', 'error', 'always', 'then', 'complete']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this part to outside the try...catch ?

I don't want to swallow an exception caused by wrapObjectKeys or something. Just bad practice since we only want to capture the exception from _oldAjax.call

@mattrobenolt
Copy link
Contributor

@matghaleb I know basically nothing about promises in jQuery, but is there a lower level primitive that we can patch here instead that gives us more coverage over other events too possibly?

@mattrobenolt
Copy link
Contributor

Like, if we can say, automatically patch all promises, no matter if they're from an XHR request or something else.

@mattrobenolt
Copy link
Contributor

We should definitely be able to patch this stuff: https://github.com/jquery/jquery/blob/master/src/deferred.js

@mattrobenolt
Copy link
Contributor

We should be able to patch jQuery.Deferred and do something. Is my guess.

@matghaleb
Copy link
Author

@mattrobenolt Thx for your feedbacks. I'll look more carefully at jQuery.Deferred code and I'll let you know as soon as possible.

@matghaleb
Copy link
Author

I just wrote a new PR #268 patching jQuery.Deferred.
Let me know your feedback about it.

@matghaleb matghaleb closed this Oct 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants