wp-util.js 3.8 KB

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