Add a callback function to the jQuery Method

Source: Internet
Author: User
[Javascript]/*** @ fileOverview this plug-in is used to add a callback function to the jQuery method, you can add any custom callback function in the class method or instance method without affecting the behavior of the original method * @ dependencyjQuery1.7 + * @ authorhuhai * @ since2013-01... syntax [javascript]/*** @ fileOverview this plug-in is used to add a callback function to the jQuery method, you can add any custom callback function in 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 ({/*** @ demo- Ion adds a callback function to the Method * @ param funcName: string the name of the callback function to be added * @ param callback: function callback function (if you want to remove the function, 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: string added callback function name * @ 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 [html]
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.