|
1248 | 1248 | // Make the request, allowing the user to override any Ajax options.
|
1249 | 1249 | var xhr = options.xhr = ajax(_.extend(params, options));
|
1250 | 1250 |
|
| 1251 | + model.trigger('request', model, xhr, options); |
| 1252 | + |
| 1253 | + var success = options.success; |
1251 | 1254 | var error = options.error;
|
1252 |
| - xhr.then(options.success, function(xhr, textStatus, errorThrown) { |
| 1255 | + var that = this; // actually needed? |
| 1256 | + |
| 1257 | + // pipe the results through "then", to gurarantee that |
| 1258 | + // options.success and options.error are called before |
| 1259 | + // the returned promise resolves. This gives the |
| 1260 | + // collections/models a chance to update first. |
| 1261 | + return xhr.then(function() { |
| 1262 | + success.apply(that, arguments); |
| 1263 | + return thenArgs(arguments); |
| 1264 | + }, function(xhr, textStatus, errorThrown) { |
1253 | 1265 | // Pass along `textStatus` and `errorThrown` from jQuery.
|
1254 | 1266 | options.textStatus = textStatus;
|
1255 | 1267 | options.errorThrown = errorThrown;
|
1256 |
| - if (error) error.apply(this, arguments); |
| 1268 | + if (error) error.apply(that, arguments); |
| 1269 | + return failArgs(arguments); |
1257 | 1270 | });
|
1258 |
| - |
1259 |
| - model.trigger('request', model, xhr, options); |
1260 |
| - return xhr; |
1261 | 1271 | };
|
1262 | 1272 |
|
1263 | 1273 | var noXhrPatch =
|
|
1701 | 1711 | };
|
1702 | 1712 | };
|
1703 | 1713 |
|
| 1714 | + var thenArgs = function() { |
| 1715 | + return $.Deferred().resolve(arguments); |
| 1716 | + }; |
| 1717 | + |
| 1718 | + var failArgs = function() { |
| 1719 | + return $.Deferred().reject(arguments); |
| 1720 | + }; |
| 1721 | + |
1704 | 1722 | return Backbone;
|
1705 | 1723 |
|
1706 | 1724 | }));
|
0 commit comments