wp/wp-includes/js/wp-util.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
--- a/wp/wp-includes/js/wp-util.js	Tue Jun 09 11:14:17 2015 +0000
+++ b/wp/wp-includes/js/wp-util.js	Mon Oct 14 17:39:30 2019 +0200
@@ -1,4 +1,6 @@
 /* global _wpUtilSettings */
+
+/** @namespace wp */
 window.wp = window.wp || {};
 
 (function ($) {
@@ -30,7 +32,7 @@
 			};
 
 		return function ( data ) {
-			compiled = compiled || _.template( $( '#tmpl-' + id ).html(), null, options );
+			compiled = compiled || _.template( $( '#tmpl-' + id ).html(),  options );
 			return compiled( data );
 		};
 	});
@@ -48,9 +50,11 @@
 		 *
 		 * Sends a POST request to WordPress.
 		 *
-		 * @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.
+		 * @param  {(string|object)} action  The slug of the action to fire in WordPress or options passed
+		 *                                   to jQuery.ajax.
+		 * @param  {object=}         data    Optional. The data to populate $_POST with.
+		 * @return {$.promise}     A jQuery promise that represents the request,
+		 *                         decorated with an abort() method.
 		 */
 		post: function( action, data ) {
 			return wp.ajax.send({
@@ -63,11 +67,14 @@
 		 *
 		 * Sends a POST request to WordPress.
 		 *
-		 * @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.
+		 * @param  {(string|object)} action  The slug of the action to fire in WordPress or options passed
+		 *                                   to jQuery.ajax.
+		 * @param  {object=}         options Optional. The options passed to jQuery.ajax.
+		 * @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 +88,7 @@
 				context: this
 			});
 
-			return $.Deferred( function( deferred ) {
+			deferred = $.Deferred( function( deferred ) {
 				// Transfer success/error callbacks.
 				if ( options.success )
 					deferred.done( options.success );
@@ -92,9 +99,8 @@
 				delete options.error;
 
 				// Use with PHP's wp_send_json_success() and wp_send_json_error()
-				$.ajax( options ).done( function( response ) {
-					// Treat a response of `1` as successful for backwards
-					// compatibility with existing handlers.
+				deferred.jqXHR = $.ajax( options ).done( function( response ) {
+					// Treat a response of 1 as successful for backward compatibility with existing handlers.
 					if ( response === '1' || response === 1 )
 						response = { success: true };
 
@@ -105,7 +111,15 @@
 				}).fail( function() {
 					deferred.rejectWith( this, arguments );
 				});
-			}).promise();
+			});
+
+			promise = deferred.promise();
+			promise.abort = function() {
+				deferred.jqXHR.abort();
+				return this;
+			};
+
+			return promise;
 		}
 	};