Code that uses the jQuery global event ajaxStart to implement the prompt effect for a specific request

Source: Internet
Author: User

Scenario
How can we achieve "ajaxStart" in a specific request?
First, the price for rewriting Ajax methods is too high, and jQuery's own Ajax Events can still be used.
Global events triggered by Ajax are transmitted to all DOM nodes like a standard event. Level: jQuery Events> Ajax Events> Custom Ajax Events.
Implementation
Copy codeThe Code is as follows:
Wo = window. Wo | {};
Wo. ajax = {
Spinner: $ ([])
, Init: function (){
Var $ spinner = this. spinner = $ ('# ajax-loading ');
Var show = function (e ){
If (e. namespace = 'wo') $ spinner. show ();
};
Var hide = function (e ){
$ Spinner. hide ();
};
$ Spinner. bind ({
'Ajaxstart. work': show
, 'Ajaxstop. work': hide
, 'Ajaxerror. work': hide
});
This. adapt (['getjson', 'get', 'post', 'ajax ']);
}
// Initiate a request
, _ Prepare: function (){
This. spinner. trigger ('ajaxstart. wo ');
}
// API batch change
, Adapt: function (fns ){
Var self = this;
$. Each (fns, function (I, fn ){
Wo [fn] = function (){
Self. _ prepare ();
$ [Fn]. apply (this, arguments );
}
});
}
};

There are two ways to determine whether the triggered event is a specific event:
The specified namespace.
When triggered, additional parameters are passed to the event handler.
Here, we use the namespace of the event to trigger the Source Judgment. The adapt method is equivalent to the application of the adapter and replaces another interface with a set of improved interfaces.
If you want to add the load method, it will be a little more troublesome, because it may be an ajax method or an onload event of an element.
You need to add a proxy method and determine the type:
Copy codeThe Code is as follows:
Var _ load = $. fn. load;
$. Fn. load = function (){
$. Type (arguments [0]) === 'string' & self. _ prepare ();
_ Load. apply (this, arguments );
Return this;
};

Use
All method parameters are still consistent with the original one:
Copy codeThe Code is as follows:
Wo. post (url, [data,] callback)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.