*
* @param {string} action The slug of the action to fire in WordPress.
* @param {object} data The data to populate $_POST with.
- * @return {$.promise} A jQuery promise that represents the request.
+ * @return {$.promise} A jQuery promise that represents the request,
+ * decorated with an abort() method.
*/
post: function( action, data ) {
return wp.ajax.send({
*
* @param {string} action The slug of the action to fire in WordPress.
* @param {object} options The options passed to jQuery.ajax.
- * @return {$.promise} A jQuery promise that represents the request.
+ * @return {$.promise} A jQuery promise that represents the request,
+ * decorated with an abort() method.
*/
send: function( action, options ) {
+ var promise, deferred;
if ( _.isObject( action ) ) {
options = action;
} else {
context: this
});
- return $.Deferred( function( deferred ) {
+ deferred = $.Deferred( function( deferred ) {
// Transfer success/error callbacks.
if ( options.success )
deferred.done( options.success );
delete options.error;
// Use with PHP's wp_send_json_success() and wp_send_json_error()
- $.ajax( options ).done( function( response ) {
+ deferred.jqXHR = $.ajax( options ).done( function( response ) {
// Treat a response of `1` as successful for backwards
// compatibility with existing handlers.
if ( response === '1' || response === 1 )
}).fail( function() {
deferred.rejectWith( this, arguments );
});
- }).promise();
+ });
+
+ promise = deferred.promise();
+ promise.abort = function() {
+ deferred.jqXHR.abort();
+ return this;
+ };
+
+ return promise;
}
};