Add a callback function plug-in to the jQuery method.

Source: Internet
Author: User
This plug-in is used to add a callback function to the jQuery method. You can add any custom callback function to the class method or instance method without affecting the behavior of the original method. For more information, see, I hope this article will help you with the plug-in source code jquery. callback. js
Plug-in Open Source Address: https://gist.github.com/4580276

The Code is as follows:


/**
* @ FileOverview this plug-in is used to add a callback function to the jQuery method. You can add any custom callback function to the class method or instance method without affecting the behavior of the original method.
* @ Dependency jQuery1.7 +
* @ Author huhai
* @ Since 2013-01-21
*/
(Function ($ ){
$. _ Callbacks = {};
$. _ Callbacks _ = {};
$. _ Alias = {};
$. _ Alias _ = {};
$. Extend ({
/**
* @ Decription Add a callback function to the Method
* @ Param funcName: string name of the callback function to be added
* @ Param callback: function callback function (if you want to remove it, do not use the anonymous method)
* @ Param static: whether the boolean is a class method. The default value is false.
*/
AddCallback: function (funcName, callback, static ){
If ("string" === typeof (funcName) & $. isFunction (callback )){
If (static = true ){
If ($ [funcName] & $. isFunction ($ [funcName]) {
If (! This. _ callbacks [funcName]) {
This. _ callbacks [funcName] = $. Callbacks ();
}
This. _ callbacks [funcName]. add (callback );
If (! $. _ Alias [funcName]) {
$. _ Alias [funcName] = $ [funcName]; // store the original Class Method

$ [FuncName] = function () {// proxy class method;
Var result = $. _ alias [funcName]. apply (this, arguments );
$. _ Callbacks [funcName]. fireWith (this, arguments );
Return result;
};
}
}
} Else {
If ($. fn [funcName] & $. isFunction ($. fn [funcName]) {
If (! This. _ callbacks _ [funcName]) {
This. _ callbacks _ [funcName] = $. Callbacks ();
}
This. _ callbacks _ [funcName]. add (callback );
If (! $. _ Alias _ [funcName]) {
$. _ Alias _ [funcName] = $. fn [funcName]; // Method for storing the original Instance
$. Fn [funcName] = function () {// proxy instance method;
Var result = $. _ alias _ [funcName]. apply (this, arguments );
$. _ Callbacks _ [funcName]. fireWith (this, arguments );
Return result;
};
}
}
}
}
},
/**
* @ Decription remove the callback function added to the Method
* @ Param funcName: name of the callback function added to the string
* @ Param callback: function callback function
* @ Param static: whether the boolean is a class method. The default value is false.
*/
RemoveCallback: function (funcName, callback, static ){
If ("string" === typeof (funcName) & $. isFunction (callback )){
If (static = true ){
If ($ [funcName] & $. isFunction ($ [funcName]) {
If (this. _ callbacks [funcName]) {
This. _ callbacks. remove (callback );
}
}
} Else {
If ($. fn [funcName] & $. isFunction ($. fn [funcName]) {
If (this. _ callbacks _ [funcName]) {
This. _ callbacks _. remove (callback );
}
}
}
}
}
});
}) (JQuery );


Usage example:
HTML

The Code is as follows:






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.