Last active
September 18, 2015 19:11
-
-
Save ianmstew/bf64793e37593a37eecb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RSVP = require('rsvp'); | |
var jQuery = require('jquery'); | |
// Log uncaught errors (important) | |
RSVP.on('error', function (reason) { | |
console.error((reason && reason.stack) || reason || 'Error'); | |
}); | |
// Shim the browser window with ES6 Promise | |
window.Promise = RSVP.Promise; | |
// Replace jQuery ajax to return an ES6 Promise | |
jQuery.ajax = function () { | |
var ajaxArgs = arguments; | |
// Return an ES6 Promise rather than a jQuery xhr | |
return new Promise(function (resolve, reject) { | |
Backbone.$.ajax.apply(Backbone.$, ajaxArgs) | |
.done(function (data, textStatus, jqXHR) { | |
resolve(data); | |
}) | |
.error(function (jqXHR, status, error) { | |
// Always resolve errors to JSON | |
reject(jqXHR.responseJSON || { error: jqXHR.responseText || jqXHR.statusText }); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment