[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/ -> wp-util.js (source)

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


Generated : Thu Apr 9 08:20:01 2020 Cross-referenced by PHPXref