wp-util.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @output wp-includes/js/wp-util.js
  3. */
  4. /* global _wpUtilSettings */
  5. /** @namespace wp */
  6. window.wp = window.wp || {};
  7. (function ($) {
  8. // Check for the utility settings.
  9. var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
  10. /**
  11. * wp.template( id )
  12. *
  13. * Fetch a JavaScript template for an id, and return a templating function for it.
  14. *
  15. * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
  16. * For example, "attachment" maps to "tmpl-attachment".
  17. * @return {function} A function that lazily-compiles the template requested.
  18. */
  19. wp.template = _.memoize(function ( id ) {
  20. var compiled,
  21. /*
  22. * Underscore's default ERB-style templates are incompatible with PHP
  23. * when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
  24. *
  25. * @see trac ticket #22344.
  26. */
  27. options = {
  28. evaluate: /<#([\s\S]+?)#>/g,
  29. interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
  30. escape: /\{\{([^\}]+?)\}\}(?!\})/g,
  31. variable: 'data'
  32. };
  33. return function ( data ) {
  34. compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options );
  35. return compiled( data );
  36. };
  37. });
  38. // wp.ajax
  39. // ------
  40. //
  41. // Tools for sending ajax requests with JSON responses and built in error handling.
  42. // Mirrors and wraps jQuery's ajax APIs.
  43. wp.ajax = {
  44. settings: settings.ajax || {},
  45. /**
  46. * wp.ajax.post( [action], [data] )
  47. *
  48. * Sends a POST request to WordPress.
  49. *
  50. * @param {(string|object)} action The slug of the action to fire in WordPress or options passed
  51. * to jQuery.ajax.
  52. * @param {object=} data Optional. The data to populate $_POST with.
  53. * @return {$.promise} A jQuery promise that represents the request,
  54. * decorated with an abort() method.
  55. */
  56. post: function( action, data ) {
  57. return wp.ajax.send({
  58. data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
  59. });
  60. },
  61. /**
  62. * wp.ajax.send( [action], [options] )
  63. *
  64. * Sends a POST request to WordPress.
  65. *
  66. * @param {(string|object)} action The slug of the action to fire in WordPress or options passed
  67. * to jQuery.ajax.
  68. * @param {object=} options Optional. The options passed to jQuery.ajax.
  69. * @return {$.promise} A jQuery promise that represents the request,
  70. * decorated with an abort() method.
  71. */
  72. send: function( action, options ) {
  73. var promise, deferred;
  74. if ( _.isObject( action ) ) {
  75. options = action;
  76. } else {
  77. options = options || {};
  78. options.data = _.extend( options.data || {}, { action: action });
  79. }
  80. options = _.defaults( options || {}, {
  81. type: 'POST',
  82. url: wp.ajax.settings.url,
  83. context: this
  84. });
  85. deferred = $.Deferred( function( deferred ) {
  86. // Transfer success/error callbacks.
  87. if ( options.success )
  88. deferred.done( options.success );
  89. if ( options.error )
  90. deferred.fail( options.error );
  91. delete options.success;
  92. delete options.error;
  93. // Use with PHP's wp_send_json_success() and wp_send_json_error()
  94. deferred.jqXHR = $.ajax( options ).done( function( response ) {
  95. // Treat a response of 1 as successful for backward compatibility with existing handlers.
  96. if ( response === '1' || response === 1 )
  97. response = { success: true };
  98. if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
  99. deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
  100. else
  101. deferred.rejectWith( this, [response] );
  102. }).fail( function() {
  103. deferred.rejectWith( this, arguments );
  104. });
  105. });
  106. promise = deferred.promise();
  107. promise.abort = function() {
  108. deferred.jqXHR.abort();
  109. return this;
  110. };
  111. return promise;
  112. }
  113. };
  114. }(jQuery));