From 93e01e68b518fd8bd66ef2fbd10ee76122f158f0 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 13 Jun 2015 14:29:42 +0000 Subject: [PATCH] Add an `abort` class method to the Promise instance returned by `wp.ajax.send()`. Props westonruter. Fixes #32628. git-svn-id: https://develop.svn.wordpress.org/trunk@32747 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/js/wp-util.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/js/wp-util.js b/src/wp-includes/js/wp-util.js index 1985b35e6f..84cb5510bb 100644 --- a/src/wp-includes/js/wp-util.js +++ b/src/wp-includes/js/wp-util.js @@ -50,7 +50,8 @@ window.wp = window.wp || {}; * * @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({ @@ -65,9 +66,11 @@ window.wp = window.wp || {}; * * @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 { @@ -81,7 +84,7 @@ window.wp = window.wp || {}; context: this }); - return $.Deferred( function( deferred ) { + deferred = $.Deferred( function( deferred ) { // Transfer success/error callbacks. if ( options.success ) deferred.done( options.success ); @@ -92,7 +95,7 @@ window.wp = window.wp || {}; 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 ) @@ -105,7 +108,15 @@ window.wp = window.wp || {}; }).fail( function() { deferred.rejectWith( this, arguments ); }); - }).promise(); + }); + + promise = deferred.promise(); + promise.abort = function() { + deferred.jqXHR.abort(); + return this; + }; + + return promise; } };