[ Index ]

PHP Cross Reference of WordPress

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      // wp.ajax
  45      // ------
  46      //
  47      // Tools for sending ajax requests with JSON responses and built in error handling.
  48      // Mirrors and wraps jQuery's ajax APIs.
  49      wp.ajax = {
  50          settings: settings.ajax || {},
  51  
  52          /**
  53           * wp.ajax.post( [action], [data] )
  54           *
  55           * Sends a POST request to WordPress.
  56           *
  57           * @param  {(string|object)} action  The slug of the action to fire in WordPress or options passed
  58           *                                   to jQuery.ajax.
  59           * @param  {object=}         data    Optional. The data to populate $_POST with.
  60           * @return {$.promise}     A jQuery promise that represents the request,
  61           *                         decorated with an abort() method.
  62           */
  63          post: function( action, data ) {
  64              return wp.ajax.send({
  65                  data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
  66              });
  67          },
  68  
  69          /**
  70           * wp.ajax.send( [action], [options] )
  71           *
  72           * Sends a POST request to WordPress.
  73           *
  74           * @param  {(string|object)} action  The slug of the action to fire in WordPress or options passed
  75           *                                   to jQuery.ajax.
  76           * @param  {object=}         options Optional. The options passed to jQuery.ajax.
  77           * @return {$.promise}      A jQuery promise that represents the request,
  78           *                          decorated with an abort() method.
  79           */
  80          send: function( action, options ) {
  81              var promise, deferred;
  82              if ( _.isObject( action ) ) {
  83                  options = action;
  84              } else {
  85                  options = options || {};
  86                  options.data = _.extend( options.data || {}, { action: action });
  87              }
  88  
  89              options = _.defaults( options || {}, {
  90                  type:    'POST',
  91                  url:     wp.ajax.settings.url,
  92                  context: this
  93              });
  94  
  95              deferred = $.Deferred( function( deferred ) {
  96                  // Transfer success/error callbacks.
  97                  if ( options.success )
  98                      deferred.done( options.success );
  99                  if ( options.error )
 100                      deferred.fail( options.error );
 101  
 102                  delete options.success;
 103                  delete options.error;
 104  
 105                  // Use with PHP's wp_send_json_success() and wp_send_json_error()
 106                  deferred.jqXHR = $.ajax( options ).done( function( response ) {
 107                      // Treat a response of 1 as successful for backward compatibility with existing handlers.
 108                      if ( response === '1' || response === 1 )
 109                          response = { success: true };
 110  
 111                      if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
 112                          deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
 113                      else
 114                          deferred.rejectWith( this, [response] );
 115                  }).fail( function() {
 116                      deferred.rejectWith( this, arguments );
 117                  });
 118              });
 119  
 120              promise = deferred.promise();
 121              promise.abort = function() {
 122                  deferred.jqXHR.abort();
 123                  return this;
 124              };
 125  
 126              return promise;
 127          }
 128      };
 129  
 130  }(jQuery));


Generated: Sat Sep 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1