14 /** |
14 /** |
15 * wp.template( id ) |
15 * wp.template( id ) |
16 * |
16 * |
17 * Fetch a JavaScript template for an id, and return a templating function for it. |
17 * Fetch a JavaScript template for an id, and return a templating function for it. |
18 * |
18 * |
19 * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-". |
19 * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-". |
20 * For example, "attachment" maps to "tmpl-attachment". |
20 * For example, "attachment" maps to "tmpl-attachment". |
21 * @return {function} A function that lazily-compiles the template requested. |
21 * @return {function} A function that lazily-compiles the template requested. |
22 */ |
22 */ |
23 wp.template = _.memoize(function ( id ) { |
23 wp.template = _.memoize(function ( id ) { |
24 var compiled, |
24 var compiled, |
25 /* |
25 /* |
26 * Underscore's default ERB-style templates are incompatible with PHP |
26 * Underscore's default ERB-style templates are incompatible with PHP |
39 compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options ); |
39 compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options ); |
40 return compiled( data ); |
40 return compiled( data ); |
41 }; |
41 }; |
42 }); |
42 }); |
43 |
43 |
44 // wp.ajax |
44 /* |
45 // ------ |
45 * wp.ajax |
46 // |
46 * ------ |
47 // Tools for sending ajax requests with JSON responses and built in error handling. |
47 * |
48 // Mirrors and wraps jQuery's ajax APIs. |
48 * Tools for sending ajax requests with JSON responses and built in error handling. |
|
49 * Mirrors and wraps jQuery's ajax APIs. |
|
50 */ |
49 wp.ajax = { |
51 wp.ajax = { |
50 settings: settings.ajax || {}, |
52 settings: settings.ajax || {}, |
51 |
53 |
52 /** |
54 /** |
53 * wp.ajax.post( [action], [data] ) |
55 * wp.ajax.post( [action], [data] ) |
54 * |
56 * |
55 * Sends a POST request to WordPress. |
57 * Sends a POST request to WordPress. |
56 * |
58 * |
57 * @param {(string|object)} action The slug of the action to fire in WordPress or options passed |
59 * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed |
58 * to jQuery.ajax. |
60 * to jQuery.ajax. |
59 * @param {object=} data Optional. The data to populate $_POST with. |
61 * @param {Object=} data Optional. The data to populate $_POST with. |
60 * @return {$.promise} A jQuery promise that represents the request, |
62 * @return {$.promise} A jQuery promise that represents the request, |
61 * decorated with an abort() method. |
63 * decorated with an abort() method. |
62 */ |
64 */ |
63 post: function( action, data ) { |
65 post: function( action, data ) { |
64 return wp.ajax.send({ |
66 return wp.ajax.send({ |
65 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action }) |
67 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action }) |
66 }); |
68 }); |
69 /** |
71 /** |
70 * wp.ajax.send( [action], [options] ) |
72 * wp.ajax.send( [action], [options] ) |
71 * |
73 * |
72 * Sends a POST request to WordPress. |
74 * Sends a POST request to WordPress. |
73 * |
75 * |
74 * @param {(string|object)} action The slug of the action to fire in WordPress or options passed |
76 * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed |
75 * to jQuery.ajax. |
77 * to jQuery.ajax. |
76 * @param {object=} options Optional. The options passed to jQuery.ajax. |
78 * @param {Object=} options Optional. The options passed to jQuery.ajax. |
77 * @return {$.promise} A jQuery promise that represents the request, |
79 * @return {$.promise} A jQuery promise that represents the request, |
78 * decorated with an abort() method. |
80 * decorated with an abort() method. |
79 */ |
81 */ |
80 send: function( action, options ) { |
82 send: function( action, options ) { |
81 var promise, deferred; |
83 var promise, deferred; |
82 if ( _.isObject( action ) ) { |
84 if ( _.isObject( action ) ) { |
83 options = action; |
85 options = action; |
92 context: this |
94 context: this |
93 }); |
95 }); |
94 |
96 |
95 deferred = $.Deferred( function( deferred ) { |
97 deferred = $.Deferred( function( deferred ) { |
96 // Transfer success/error callbacks. |
98 // Transfer success/error callbacks. |
97 if ( options.success ) |
99 if ( options.success ) { |
98 deferred.done( options.success ); |
100 deferred.done( options.success ); |
99 if ( options.error ) |
101 } |
|
102 |
|
103 if ( options.error ) { |
100 deferred.fail( options.error ); |
104 deferred.fail( options.error ); |
|
105 } |
101 |
106 |
102 delete options.success; |
107 delete options.success; |
103 delete options.error; |
108 delete options.error; |
104 |
109 |
105 // Use with PHP's wp_send_json_success() and wp_send_json_error() |
110 // Use with PHP's wp_send_json_success() and wp_send_json_error(). |
106 deferred.jqXHR = $.ajax( options ).done( function( response ) { |
111 deferred.jqXHR = $.ajax( options ).done( function( response ) { |
107 // Treat a response of 1 as successful for backward compatibility with existing handlers. |
112 // Treat a response of 1 as successful for backward compatibility with existing handlers. |
108 if ( response === '1' || response === 1 ) |
113 if ( response === '1' || response === 1 ) { |
109 response = { success: true }; |
114 response = { success: true }; |
|
115 } |
110 |
116 |
111 if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) |
117 if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) { |
112 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] ); |
118 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] ); |
113 else |
119 } else { |
114 deferred.rejectWith( this, [response] ); |
120 deferred.rejectWith( this, [response] ); |
|
121 } |
115 }).fail( function() { |
122 }).fail( function() { |
116 deferred.rejectWith( this, arguments ); |
123 deferred.rejectWith( this, arguments ); |
117 }); |
124 }); |
118 }); |
125 }); |
119 |
126 |