Make WordPress Core

Changeset 24369


Ignore:
Timestamp:
05/26/2013 06:58:01 AM (13 years ago)
Author:
koopersmith
Message:

Move XHR helpers from media to utils. See #24424.

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/media-models.js

    r24366 r24369  
    9494         *
    9595         * Fetches a template by id.
    96          * See wp.template() in `wp-includes/js/wp-backbone.js`.
     96         * See wp.template() in `wp-includes/js/wp-util.js`.
    9797         */
    9898        template: wp.template,
     
    102102         *
    103103         * Sends a POST request to WordPress.
    104          *
    105          * @param  {string} action The slug of the action to fire in WordPress.
    106          * @param  {object} data   The data to populate $_POST with.
    107          * @return {$.promise}     A jQuery promise that represents the request.
     104         * See wp.xhr.post() in `wp-includes/js/wp-util.js`.
    108105         */
    109         post: function( action, data ) {
    110             return media.ajax({
    111                 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
    112             });
    113         },
     106        post: wp.xhr.post,
    114107
    115108        /**
    116109         * media.ajax( [action], [options] )
    117110         *
    118          * Sends a POST request to WordPress.
    119          *
    120          * @param  {string} action  The slug of the action to fire in WordPress.
    121          * @param  {object} options The options passed to jQuery.ajax.
    122          * @return {$.promise}      A jQuery promise that represents the request.
     111         * Sends an XHR request to WordPress.
     112         * See wp.xhr.send() in `wp-includes/js/wp-util.js`.
    123113         */
    124         ajax: function( action, options ) {
    125             if ( _.isObject( action ) ) {
    126                 options = action;
    127             } else {
    128                 options = options || {};
    129                 options.data = _.extend( options.data || {}, { action: action });
    130             }
    131 
    132             options = _.defaults( options || {}, {
    133                 type:    'POST',
    134                 url:     media.model.settings.ajaxurl,
    135                 context: this
    136             });
    137 
    138             return $.Deferred( function( deferred ) {
    139                 // Transfer success/error callbacks.
    140                 if ( options.success )
    141                     deferred.done( options.success );
    142                 if ( options.error )
    143                     deferred.fail( options.error );
    144 
    145                 delete options.success;
    146                 delete options.error;
    147 
    148                 // Use with PHP's wp_send_json_success() and wp_send_json_error()
    149                 $.ajax( options ).done( function( response ) {
    150                     // Treat a response of `1` as successful for backwards
    151                     // compatibility with existing handlers.
    152                     if ( response === '1' || response === 1 )
    153                         response = { success: true };
    154 
    155                     if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
    156                         deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
    157                     else
    158                         deferred.rejectWith( this, [response] );
    159                 }).fail( function() {
    160                     deferred.rejectWith( this, arguments );
    161                 });
    162             }).promise();
    163         },
     114        ajax: wp.xhr.send,
    164115
    165116        // Scales a set of dimensions to fit within bounding dimensions.
  • trunk/wp-includes/js/wp-util.js

    r24368 r24369  
    22
    33(function ($) {
     4    // Check for the utility settings.
     5    var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
     6
    47    /**
    58     * wp.template( id )
     
    2528        };
    2629    });
     30
     31    // wp.xhr
     32    // ------
     33    //
     34    // Tools for sending ajax requests with JSON responses and built in error handling.
     35    // Mirrors and wraps jQuery's ajax APIs.
     36    wp.xhr = {
     37        settings: settings.xhr || {},
     38
     39        /**
     40         * wp.xhr.post( [action], [data] )
     41         *
     42         * Sends a POST request to WordPress.
     43         *
     44         * @param  {string} action The slug of the action to fire in WordPress.
     45         * @param  {object} data   The data to populate $_POST with.
     46         * @return {$.promise}     A jQuery promise that represents the request.
     47         */
     48        post: function( action, data ) {
     49            return wp.xhr.send({
     50                data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
     51            });
     52        },
     53
     54        /**
     55         * wp.xhr.send( [action], [options] )
     56         *
     57         * Sends a POST request to WordPress.
     58         *
     59         * @param  {string} action  The slug of the action to fire in WordPress.
     60         * @param  {object} options The options passed to jQuery.ajax.
     61         * @return {$.promise}      A jQuery promise that represents the request.
     62         */
     63        send: function( action, options ) {
     64            if ( _.isObject( action ) ) {
     65                options = action;
     66            } else {
     67                options = options || {};
     68                options.data = _.extend( options.data || {}, { action: action });
     69            }
     70
     71            options = _.defaults( options || {}, {
     72                type:    'POST',
     73                url:     wp.xhr.settings.url,
     74                context: this
     75            });
     76
     77            return $.Deferred( function( deferred ) {
     78                // Transfer success/error callbacks.
     79                if ( options.success )
     80                    deferred.done( options.success );
     81                if ( options.error )
     82                    deferred.fail( options.error );
     83
     84                delete options.success;
     85                delete options.error;
     86
     87                // Use with PHP's wp_send_json_success() and wp_send_json_error()
     88                $.ajax( options ).done( function( response ) {
     89                    // Treat a response of `1` as successful for backwards
     90                    // compatibility with existing handlers.
     91                    if ( response === '1' || response === 1 )
     92                        response = { success: true };
     93
     94                    if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
     95                        deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
     96                    else
     97                        deferred.rejectWith( this, [response] );
     98                }).fail( function() {
     99                    deferred.rejectWith( this, arguments );
     100                });
     101            }).promise();
     102        }
     103    };
     104
    27105}(jQuery));
  • trunk/wp-includes/script-loader.php

    r24368 r24369  
    276276
    277277    $scripts->add( 'wp-util', "/wp-includes/js/wp-util$suffix.js", array('underscore', 'jquery'), false, 1 );
     278    did_action( 'init' ) && $scripts->localize( 'wp-util', '_wpUtilSettings', array(
     279        'xhr' => array(
     280            'url' => admin_url( 'admin-ajax.php', 'relative' ),
     281        ),
     282    ) );
     283
    278284    $scripts->add( 'wp-backbone', "/wp-includes/js/wp-backbone$suffix.js", array('backbone', 'wp-util'), false, 1 );
    279285
Note: See TracChangeset for help on using the changeset viewer.