Changeset 24369
- Timestamp:
- 05/26/2013 06:58:01 AM (13 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 3 edited
-
js/media-models.js (modified) (2 diffs)
-
js/wp-util.js (modified) (2 diffs)
-
script-loader.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/js/media-models.js
r24366 r24369 94 94 * 95 95 * 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`. 97 97 */ 98 98 template: wp.template, … … 102 102 * 103 103 * 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`. 108 105 */ 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, 114 107 115 108 /** 116 109 * media.ajax( [action], [options] ) 117 110 * 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`. 123 113 */ 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, 164 115 165 116 // Scales a set of dimensions to fit within bounding dimensions. -
trunk/wp-includes/js/wp-util.js
r24368 r24369 2 2 3 3 (function ($) { 4 // Check for the utility settings. 5 var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings; 6 4 7 /** 5 8 * wp.template( id ) … … 25 28 }; 26 29 }); 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 27 105 }(jQuery)); -
trunk/wp-includes/script-loader.php
r24368 r24369 276 276 277 277 $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 278 284 $scripts->add( 'wp-backbone', "/wp-includes/js/wp-backbone$suffix.js", array('backbone', 'wp-util'), false, 1 ); 279 285
Note: See TracChangeset
for help on using the changeset viewer.