Вход Регистрация
Файл: wordpress/wp-includes/js/wp-util.js
Строк: 143
<?php
/* global _wpUtilSettings */
window.wp window.wp || {};

(function ($) {
    
// Check for the utility settings.
    
var settings typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;

    
/**
     * wp.template( id )
     *
     * Fetch a JavaScript template for an id, and return a templating function for it.
     *
     * @param  {string} id   A string that corresponds to a DOM element with an id prefixed with "tmpl-".
     *                       For example, "attachment" maps to "tmpl-attachment".
     * @return {function}    A function that lazily-compiles the template requested.
     */
    
wp.template _.memoize(function ( id ) {
        var 
compiled,
            
/*
             * Underscore's default ERB-style templates are incompatible with PHP
             * when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
             *
             * @see trac ticket #22344.
             */
            
options = {
                
evaluate:    /<#([sS]+?)#>/g,
                
interpolate: /{{{([sS]+?)}}}/g,
                
escape:      /{{([^}]+?)}}(?!})/g,
                
variable:    'data'
            
};

        return function ( 
data ) {
            
compiled compiled || _.template( $( '#tmpl-' id ).html(), nulloptions );
            return 
compileddata );
        };
    });

    
// wp.ajax
    // ------
    //
    // Tools for sending ajax requests with JSON responses and built in error handling.
    // Mirrors and wraps jQuery's ajax APIs.
    
wp.ajax = {
        
settingssettings.ajax || {},

        
/**
         * wp.ajax.post( [action], [data] )
         *
         * Sends a POST request to WordPress.
         *
         * @param  {string} action The slug of the action to fire in WordPress.
         * @param  {object} data   The data to populate $_POST with.
         * @return {$.promise}     A jQuery promise that represents the request.
         */
        
post: function( actiondata ) {
            return 
wp.ajax.send({
                
data_.isObjectaction ) ? action _.extenddata || {}, { actionaction })
            });
        },

        
/**
         * wp.ajax.send( [action], [options] )
         *
         * Sends a POST request to WordPress.
         *
         * @param  {string} action  The slug of the action to fire in WordPress.
         * @param  {object} options The options passed to jQuery.ajax.
         * @return {$.promise}      A jQuery promise that represents the request.
         */
        
send: function( actionoptions ) {
            if ( 
_.isObjectaction ) ) {
                
options action;
            } else {
                
options options || {};
                
options.data _.extendoptions.data || {}, { actionaction });
            }

            
options _.defaultsoptions || {}, {
                
type:    'POST',
                
url:     wp.ajax.settings.url,
                
contextthis
            
});

            return $.
Deferred( function( deferred ) {
                
// Transfer success/error callbacks.
                
if ( options.success )
                    
deferred.doneoptions.success );
                if ( 
options.error )
                    
deferred.failoptions.error );

                
delete options.success;
                
delete options.error;

                
// Use with PHP's wp_send_json_success() and wp_send_json_error()
                
$.ajaxoptions ).done( function( response ) {
                    
// Treat a response of `1` as successful for backwards
                    // compatibility with existing handlers.
                    
if ( response === '1' || response === )
                        
response = { successtrue };

                    if ( 
_.isObjectresponse ) && ! _.isUndefinedresponse.success ) )
                        
deferredresponse.success 'resolveWith' 'rejectWith' ]( this, [response.data] );
                    else
                        
deferred.rejectWiththis, [response] );
                }).
fail( function() {
                    
deferred.rejectWiththisarguments );
                });
            }).
promise();
        }
    };

}(
jQuery));
?>
Онлайн: 3
Реклама