Skip to content

Commit 438c402

Browse files
committed
Wrap $.ajax promise api ...
fixes #326
1 parent 00eef8d commit 438c402

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

plugins/jquery.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,25 @@ $.fn.ready = function ravenjQueryReadyWrapper(fn) {
4242
return _oldReady.call(this, Raven.wrap(fn));
4343
};
4444

45+
function wrapFunctionProperties(obj, keys) {
46+
var key, i;
47+
for (i = 0; i < keys.length; i++) {
48+
key = keys[i];
49+
if ($.isFunction(obj[key])) {
50+
obj[key] = Raven.wrap(obj[key]);
51+
}
52+
}
53+
}
54+
4555
var _oldAjax = $.ajax;
4656
$.ajax = function ravenAjaxWrapper(url, options) {
47-
var keys = ['complete', 'error', 'success'], key;
57+
var optionsCallbacks = ['complete', 'error', 'success'],
58+
promiseCallbacks = [
59+
'done', 'fail', 'always', 'then', // documented
60+
'success', 'error', 'complete' // deprecated
61+
],
62+
jqxhr;
63+
4864

4965
// Taken from https://github.com/jquery/jquery/blob/eee2eaf1d7a189d99106423a4206c224ebd5b848/src/ajax.js#L311-L318
5066
// If url is an object, simulate pre-1.5 signature
@@ -56,16 +72,14 @@ $.ajax = function ravenAjaxWrapper(url, options) {
5672
// Force options to be an object
5773
options = options || {};
5874

59-
/*jshint -W084*/
60-
while(key = keys.pop()) {
61-
if ($.isFunction(options[key])) {
62-
options[key] = Raven.wrap(options[key]);
63-
}
64-
}
65-
/*jshint +W084*/
75+
wrapFunctionProperties(options, optionsCallbacks);
6676

77+
78+
/*jshint +W084*/
6779
try {
68-
return _oldAjax.call(this, url, options);
80+
jqxhr = _oldAjax.call(this, url, options);
81+
wrapFunctionProperties(jqxhr, promiseCallbacks);
82+
return jqxhr;
6983
} catch (e) {
7084
Raven.captureException(e);
7185
throw e;

0 commit comments

Comments
 (0)