Skip to content

Commit cf54088

Browse files
committed
Use ajax's "then" instead of {success:...,error:...}
This allows for implementors of ajax functions to not need to look for success, error callbacks in their options.
1 parent 9a83178 commit cf54088

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

backbone.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1242,19 +1242,20 @@
12421242
};
12431243
}
12441244

1245-
// Pass along `textStatus` and `errorThrown` from jQuery.
1246-
var error = options.error;
1247-
options.error = function(xhr, textStatus, errorThrown) {
1248-
options.textStatus = textStatus;
1249-
options.errorThrown = errorThrown;
1250-
if (error) error.apply(this, arguments);
1251-
};
1252-
12531245
// Allow one-time override of the ajax function.
12541246
var ajax = options.ajax || Backbone.ajax;
12551247

12561248
// Make the request, allowing the user to override any Ajax options.
12571249
var xhr = options.xhr = ajax(_.extend(params, options));
1250+
1251+
var error = options.error;
1252+
xhr.then(options.success, function(xhr, textStatus, errorThrown) {
1253+
// Pass along `textStatus` and `errorThrown` from jQuery.
1254+
options.textStatus = textStatus;
1255+
options.errorThrown = errorThrown;
1256+
if (error) error.apply(this, arguments);
1257+
});
1258+
12581259
model.trigger('request', model, xhr, options);
12591260
return xhr;
12601261
};

test/collection.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@
784784
test("#1412 - Trigger 'request' and 'sync' events.", 4, function() {
785785
var collection = new Backbone.Collection;
786786
collection.url = '/test';
787-
Backbone.ajax = function(settings){ settings.success(); };
787+
Backbone.ajax = function(settings){ return $.Deferred().resolve(); };
788788

789789
collection.on('request', function(obj, xhr, options) {
790790
ok(obj === collection, "collection has correct 'request' event after fetching");
@@ -1151,8 +1151,9 @@
11511151
}));
11521152
var ajax = Backbone.ajax;
11531153
Backbone.ajax = function (params) {
1154-
_.defer(params.success);
1155-
return {someHeader: 'headerValue'};
1154+
var dfd = $.Deferred();
1155+
_.defer(dfd.resolve);
1156+
return $.extend({someHeader: 'headerValue'}, dfd);
11561157
};
11571158
collection.fetch({
11581159
success: function () { start(); }

test/environment.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// Capture ajax settings for comparison.
1818
Backbone.ajax = function(settings) {
1919
env.ajaxSettings = settings;
20+
return $.Deferred();
2021
};
2122

2223
// Capture the arguments to Backbone.sync for comparison.
@@ -26,7 +27,7 @@
2627
model: model,
2728
options: options
2829
};
29-
sync.apply(this, arguments);
30+
return sync.apply(this, arguments);
3031
};
3132

3233
});

test/sync.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
test("Backbone.ajax", 1, function() {
148148
Backbone.ajax = function(settings){
149149
strictEqual(settings.url, '/test');
150+
return $.Deferred();
150151
};
151152
var model = new Backbone.Model();
152153
model.url = '/test';
@@ -214,8 +215,8 @@
214215
strictEqual(options.textStatus, 'textStatus');
215216
strictEqual(options.errorThrown, 'errorThrown');
216217
});
217-
model.fetch();
218-
this.ajaxSettings.error({}, 'textStatus', 'errorThrown');
218+
var rawDeferred = model.fetch();
219+
rawDeferred.reject(rawDeferred, 'textStatus', 'errorThrown');
219220
});
220221

221222
})();

0 commit comments

Comments
 (0)