Add an abort class method to the Promise instance returned by wp.ajax.send().

Props westonruter.
Fixes #32628.

Built from https://develop.svn.wordpress.org/trunk@32747


git-svn-id: http://core.svn.wordpress.org/trunk@32718 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-06-13 14:30:26 +00:00
parent 33c1c76001
commit 144a28a5c2
3 changed files with 18 additions and 7 deletions

View File

@ -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;
}
};

View File

@ -1 +1 @@
window.wp=window.wp||{},function(a){var b="undefined"==typeof _wpUtilSettings?{}:_wpUtilSettings;wp.template=_.memoize(function(b){var c,d={evaluate:/<#([\s\S]+?)#>/g,interpolate:/\{\{\{([\s\S]+?)\}\}\}/g,escape:/\{\{([^\}]+?)\}\}(?!\})/g,variable:"data"};return function(e){return(c=c||_.template(a("#tmpl-"+b).html(),null,d))(e)}}),wp.ajax={settings:b.ajax||{},post:function(a,b){return wp.ajax.send({data:_.isObject(a)?a:_.extend(b||{},{action:a})})},send:function(b,c){return _.isObject(b)?c=b:(c=c||{},c.data=_.extend(c.data||{},{action:b})),c=_.defaults(c||{},{type:"POST",url:wp.ajax.settings.url,context:this}),a.Deferred(function(b){c.success&&b.done(c.success),c.error&&b.fail(c.error),delete c.success,delete c.error,a.ajax(c).done(function(a){("1"===a||1===a)&&(a={success:!0}),_.isObject(a)&&!_.isUndefined(a.success)?b[a.success?"resolveWith":"rejectWith"](this,[a.data]):b.rejectWith(this,[a])}).fail(function(){b.rejectWith(this,arguments)})}).promise()}}}(jQuery);
window.wp=window.wp||{},function(a){var b="undefined"==typeof _wpUtilSettings?{}:_wpUtilSettings;wp.template=_.memoize(function(b){var c,d={evaluate:/<#([\s\S]+?)#>/g,interpolate:/\{\{\{([\s\S]+?)\}\}\}/g,escape:/\{\{([^\}]+?)\}\}(?!\})/g,variable:"data"};return function(e){return(c=c||_.template(a("#tmpl-"+b).html(),null,d))(e)}}),wp.ajax={settings:b.ajax||{},post:function(a,b){return wp.ajax.send({data:_.isObject(a)?a:_.extend(b||{},{action:a})})},send:function(b,c){var d,e;return _.isObject(b)?c=b:(c=c||{},c.data=_.extend(c.data||{},{action:b})),c=_.defaults(c||{},{type:"POST",url:wp.ajax.settings.url,context:this}),e=a.Deferred(function(b){c.success&&b.done(c.success),c.error&&b.fail(c.error),delete c.success,delete c.error,b.jqXHR=a.ajax(c).done(function(a){("1"===a||1===a)&&(a={success:!0}),_.isObject(a)&&!_.isUndefined(a.success)?b[a.success?"resolveWith":"rejectWith"](this,[a.data]):b.rejectWith(this,[a])}).fail(function(){b.rejectWith(this,arguments)})}),d=e.promise(),d.abort=function(){return e.jqXHR.abort(),this},d}}}(jQuery);

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.3-alpha-32746';
$wp_version = '4.3-alpha-32747';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.