WordPress.org

Make WordPress Core

Changeset 32747


Ignore:
Timestamp:
06/13/2015 02:29:42 PM (7 years ago)
Author:
wonderboymusic
Message:

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

Props westonruter.
Fixes #32628.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-util.js

    r31471 r32747  
    5151         * @param  {string} action The slug of the action to fire in WordPress.
    5252         * @param  {object} data   The data to populate $_POST with.
    53          * @return {$.promise}     A jQuery promise that represents the request.
     53         * @return {$.promise}     A jQuery promise that represents the request,
     54         *                         decorated with an abort() method.
    5455         */
    5556        post: function( action, data ) {
     
    6667         * @param  {string} action  The slug of the action to fire in WordPress.
    6768         * @param  {object} options The options passed to jQuery.ajax.
    68          * @return {$.promise}      A jQuery promise that represents the request.
     69         * @return {$.promise}      A jQuery promise that represents the request,
     70         *                          decorated with an abort() method.
    6971         */
    7072        send: function( action, options ) {
     73            var promise, deferred;
    7174            if ( _.isObject( action ) ) {
    7275                options = action;
     
    8285            });
    8386
    84             return $.Deferred( function( deferred ) {
     87            deferred = $.Deferred( function( deferred ) {
    8588                // Transfer success/error callbacks.
    8689                if ( options.success )
     
    9396
    9497                // Use with PHP's wp_send_json_success() and wp_send_json_error()
    95                 $.ajax( options ).done( function( response ) {
     98                deferred.jqXHR = $.ajax( options ).done( function( response ) {
    9699                    // Treat a response of `1` as successful for backwards
    97100                    // compatibility with existing handlers.
     
    106109                    deferred.rejectWith( this, arguments );
    107110                });
    108             }).promise();
     111            });
     112
     113            promise = deferred.promise();
     114            promise.abort = function() {
     115                deferred.jqXHR.abort();
     116                return this;
     117            };
     118
     119            return promise;
    109120        }
    110121    };
Note: See TracChangeset for help on using the changeset viewer.