wp/wp-includes/js/wp-util.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 /* global _wpUtilSettings */
     1 /* global _wpUtilSettings */
       
     2 
       
     3 /** @namespace wp */
     2 window.wp = window.wp || {};
     4 window.wp = window.wp || {};
     3 
     5 
     4 (function ($) {
     6 (function ($) {
     5 	// Check for the utility settings.
     7 	// Check for the utility settings.
     6 	var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
     8 	var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
    28 				escape:      /\{\{([^\}]+?)\}\}(?!\})/g,
    30 				escape:      /\{\{([^\}]+?)\}\}(?!\})/g,
    29 				variable:    'data'
    31 				variable:    'data'
    30 			};
    32 			};
    31 
    33 
    32 		return function ( data ) {
    34 		return function ( data ) {
    33 			compiled = compiled || _.template( $( '#tmpl-' + id ).html(), null, options );
    35 			compiled = compiled || _.template( $( '#tmpl-' + id ).html(),  options );
    34 			return compiled( data );
    36 			return compiled( data );
    35 		};
    37 		};
    36 	});
    38 	});
    37 
    39 
    38 	// wp.ajax
    40 	// wp.ajax
    46 		/**
    48 		/**
    47 		 * wp.ajax.post( [action], [data] )
    49 		 * wp.ajax.post( [action], [data] )
    48 		 *
    50 		 *
    49 		 * Sends a POST request to WordPress.
    51 		 * Sends a POST request to WordPress.
    50 		 *
    52 		 *
    51 		 * @param  {string} action The slug of the action to fire in WordPress.
    53 		 * @param  {(string|object)} action  The slug of the action to fire in WordPress or options passed
    52 		 * @param  {object} data   The data to populate $_POST with.
    54 		 *                                   to jQuery.ajax.
    53 		 * @return {$.promise}     A jQuery promise that represents the request.
    55 		 * @param  {object=}         data    Optional. The data to populate $_POST with.
       
    56 		 * @return {$.promise}     A jQuery promise that represents the request,
       
    57 		 *                         decorated with an abort() method.
    54 		 */
    58 		 */
    55 		post: function( action, data ) {
    59 		post: function( action, data ) {
    56 			return wp.ajax.send({
    60 			return wp.ajax.send({
    57 				data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
    61 				data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
    58 			});
    62 			});
    61 		/**
    65 		/**
    62 		 * wp.ajax.send( [action], [options] )
    66 		 * wp.ajax.send( [action], [options] )
    63 		 *
    67 		 *
    64 		 * Sends a POST request to WordPress.
    68 		 * Sends a POST request to WordPress.
    65 		 *
    69 		 *
    66 		 * @param  {string} action  The slug of the action to fire in WordPress.
    70 		 * @param  {(string|object)} action  The slug of the action to fire in WordPress or options passed
    67 		 * @param  {object} options The options passed to jQuery.ajax.
    71 		 *                                   to jQuery.ajax.
    68 		 * @return {$.promise}      A jQuery promise that represents the request.
    72 		 * @param  {object=}         options Optional. The options passed to jQuery.ajax.
       
    73 		 * @return {$.promise}      A jQuery promise that represents the request,
       
    74 		 *                          decorated with an abort() method.
    69 		 */
    75 		 */
    70 		send: function( action, options ) {
    76 		send: function( action, options ) {
       
    77 			var promise, deferred;
    71 			if ( _.isObject( action ) ) {
    78 			if ( _.isObject( action ) ) {
    72 				options = action;
    79 				options = action;
    73 			} else {
    80 			} else {
    74 				options = options || {};
    81 				options = options || {};
    75 				options.data = _.extend( options.data || {}, { action: action });
    82 				options.data = _.extend( options.data || {}, { action: action });
    79 				type:    'POST',
    86 				type:    'POST',
    80 				url:     wp.ajax.settings.url,
    87 				url:     wp.ajax.settings.url,
    81 				context: this
    88 				context: this
    82 			});
    89 			});
    83 
    90 
    84 			return $.Deferred( function( deferred ) {
    91 			deferred = $.Deferred( function( deferred ) {
    85 				// Transfer success/error callbacks.
    92 				// Transfer success/error callbacks.
    86 				if ( options.success )
    93 				if ( options.success )
    87 					deferred.done( options.success );
    94 					deferred.done( options.success );
    88 				if ( options.error )
    95 				if ( options.error )
    89 					deferred.fail( options.error );
    96 					deferred.fail( options.error );
    90 
    97 
    91 				delete options.success;
    98 				delete options.success;
    92 				delete options.error;
    99 				delete options.error;
    93 
   100 
    94 				// Use with PHP's wp_send_json_success() and wp_send_json_error()
   101 				// Use with PHP's wp_send_json_success() and wp_send_json_error()
    95 				$.ajax( options ).done( function( response ) {
   102 				deferred.jqXHR = $.ajax( options ).done( function( response ) {
    96 					// Treat a response of `1` as successful for backwards
   103 					// Treat a response of 1 as successful for backward compatibility with existing handlers.
    97 					// compatibility with existing handlers.
       
    98 					if ( response === '1' || response === 1 )
   104 					if ( response === '1' || response === 1 )
    99 						response = { success: true };
   105 						response = { success: true };
   100 
   106 
   101 					if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
   107 					if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
   102 						deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
   108 						deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
   103 					else
   109 					else
   104 						deferred.rejectWith( this, [response] );
   110 						deferred.rejectWith( this, [response] );
   105 				}).fail( function() {
   111 				}).fail( function() {
   106 					deferred.rejectWith( this, arguments );
   112 					deferred.rejectWith( this, arguments );
   107 				});
   113 				});
   108 			}).promise();
   114 			});
       
   115 
       
   116 			promise = deferred.promise();
       
   117 			promise.abort = function() {
       
   118 				deferred.jqXHR.abort();
       
   119 				return this;
       
   120 			};
       
   121 
       
   122 			return promise;
   109 		}
   123 		}
   110 	};
   124 	};
   111 
   125 
   112 }(jQuery));
   126 }(jQuery));