Table of Contents
Magic upgrade
Home Web Front-end JS Tutorial A brief discussion on jQuery's custom binding

A brief discussion on jQuery's custom binding

Jun 19, 2017 am 10:52 AM
jquery binding customize

jqueryCustom binding

First let's take a look at how to use jQuery's custom binding. You can use bind or live to subscribe to an event (Of course, on can also be used after 1.7), the code is as follows:


1

2

$("#myElement").bind('customEventName',function(e){ ... });

$(".elementsClass").live('customEventName',function(e){ ... });

Copy after login


Then trigger the event in the following way:


1

$("#myelement").trigger('customEventName');

Copy after login


Or you can add additional parameters to the custom event, for example:


1

2

$("#myelement").bind('customEventName',function(e,data){ if(data.custom) ... });

$("#myelement").trigger('customEventName',{ custom: false });

Copy after login


Magic upgrade

The so-called magic upgrade actually means that all custom events in the entire program can be automatically registered and bound to jQuery, and then executed When , all modules that registered for this event will be executed. For example, the UserUpdate method defined in the module User.js and the BlogUpdate method defined in Blogs.js both define the functionfunction that needs to be executed when publishing a blog. We can bind to a container specified by jQuery (such as document) by registering a unified event name (such as BlogAdded), and then after successfully publishing the blog, execute $(document).trigger("BlodAdded") That's OK.

Below we give a general sample code:


1

2

3

4

5

6

7

8

    var components = [User, Blog, Group, Friend, Topic, Photo];

var eventTypes = ["AddComplete""UpdateComplete""DeleteComplete""LockComplete""UnLockComplete"];

      $.each(components, function(i,component) {

        $.each(eventTypes, function(i,eventType) {

var handler = component[eventType];

if (handler) $(document).bind(eventType, handler);

        });

      })

Copy after login


Then the code defined by each js moduleInstallationThe following format:


1

2

3

4

5

6

7

8

    User= {

      AddComplete: function(e, data) {

//...

      },

      UpdateComplete: function(e, data) {

//...

      }

    }

Copy after login


##In this way, no matter where we are, if we need If so, we can use jQuery to trigger our event:


1

$(document).trigger("UpdateComplete", data);

Copy after login


Through this method, you can discover a module The method can only register one event. If we register multiple event triggers through one method, we can use the following method:


1

2

3

4

5

6

    var blogController = {

      blogAddOrUpdateComplete: function() {

//...

      }

    }

    blogController.blogAddComplete = blogController.blogUpdateComplete = blogController.blogAddOrUpdateComplete;

Copy after login


Final note: This article just shows a simple example. Do not mix the usage of the same event name in different modules. For example, AddComplete in User.js and AddComplete in Blog.js may not be used at all. It doesn't matter, that is to say, it only processes its own corresponding logic. At this time, this event should not be processed uniformly, but if the things to be detected are the same, it can be used, such as DisableUserComplete, which can be used universally, because the User module needs To handle operations after disabling an account, the Blog module may also need to handle operations after disabling an account.

Conclusion

Uncle’s Notes: Aimed at recording various tips and materials (including but not limited to technology) in daily work, if they are useful to you , please recommend one to give uncle the motivation to write

The above is the detailed content of A brief discussion on jQuery's custom binding. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to bind a sub-account on Xiaohongshu? How does it check whether the account is normal? How to bind a sub-account on Xiaohongshu? How does it check whether the account is normal? Mar 21, 2024 pm 10:11 PM

In today's era of information explosion, the construction of personal brand and corporate image has become increasingly important. As the leading fashion life sharing platform in China, Xiaohongshu has attracted a large number of user attention and participation. For those users who want to expand their influence and improve the efficiency of content dissemination, binding sub-accounts has become an effective means. So, how does Xiaohongshu bind a sub-account? How to check whether the account is normal? This article will answer these questions for you in detail. 1. How to bind a sub-account on Xiaohongshu? 1. Log in to your main account: First, you need to log in to your Xiaohongshu main account. 2. Open the settings menu: click "Me" in the upper right corner, and then select "Settings". 3. Enter account management: In the settings menu, find the "Account Management" or "Account Assistant" option and click

Steps and methods to bind Douyin in Toutiao Steps and methods to bind Douyin in Toutiao Mar 22, 2024 pm 05:56 PM

1. Open Toutiao. 2. Click My in the lower right corner. 3. Click [System Settings]. 4. Click [Account and Privacy Settings]. 5. Click the button on the right side of [Douyin] to bind Douyin.

The operation process of edius custom screen layout The operation process of edius custom screen layout Mar 27, 2024 pm 06:50 PM

1. The picture below is the default screen layout of edius. The default EDIUS window layout is a horizontal layout. Therefore, in a single-monitor environment, many windows overlap and the preview window is in single-window mode. 2. You can enable [Dual Window Mode] through the [View] menu bar to make the preview window display the playback window and recording window at the same time. 3. You can restore the default screen layout through [View menu bar>Window Layout>General]. In addition, you can also customize the layout that suits you and save it as a commonly used screen layout: drag the window to a layout that suits you, then click [View > Window Layout > Save Current Layout > New], and in the pop-up [Save Current Layout] Layout] enter the layout name in the small window and click OK

How to bind the Cainiao app to Pinduoduo? How to add the Cainiao Wrap to Pinduoduo platform? How to bind the Cainiao app to Pinduoduo? How to add the Cainiao Wrap to Pinduoduo platform? Mar 19, 2024 pm 02:30 PM

The Cainiao app is a platform that can provide you with various logistics information. The functions here are very powerful and easy to use. If you have any logistics-related problems, they can be solved here. Anyway, it can bring you a The one-stop service can solve everything in time. Checking the express delivery, picking up the express delivery, sending the express delivery, etc. are all without any problems. We have cooperated with various platforms and all the information can be queried. However, sometimes It will happen that the goods purchased on Pinduoduo cannot display the logistics information. In fact, you need to manually bind Pinduoduo to achieve this. The specific methods have been sorted out below, and everyone can take a look. . How to bind Cainiao to Pinduoduo account: 1. Open Cainiao APP and go to the main page

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to customize x-axis and y-axis in excel? (How to customize excel axis scale) How to customize x-axis and y-axis in excel? (How to customize excel axis scale) Mar 14, 2024 pm 02:10 PM

In an excel table, sometimes you may need to insert coordinate axes to see the changing trend of the data more intuitively. Some friends still don’t know how to insert coordinate axes in the table. Next, I will share with you how to customize the coordinate axis scale in Excel. Coordinate axis insertion method: 1. In the excel interface, select the data. 2. In the insertion interface, click to insert a column chart or bar chart. 3. In the expanded interface, select the graphic type. 4. In the right-click interface of the table, click Select Data. 5. In the expanded interface, you can customize it.

How to bind the Cainiao APP to Pinduoduo How to bind the Cainiao APP to Pinduoduo How to bind the Cainiao APP to Pinduoduo How to bind the Cainiao APP to Pinduoduo Mar 19, 2024 pm 05:16 PM

Do you know how to bind Pinduoduo when using Cainiao Wrap? The official version of Cainiao Wrap App does not automatically synchronize some Pinduoduo’s logistics information on this platform. All we need to do is You can copy the order number or check your mobile phone to see if there is any express delivery information. Of course, these all need to be completed manually. If you want to know more, come and take a look with the editor. How to bind the Cainiao APP to Pinduoduo 1. Open the Cainiao APP and click "Package Guide" in the upper left corner of the main page. 2. In the interface, there are many shopping websites, and accounts can be bound. 3. Click to import other e-commerce platforms. 4. User authorization: Click Pinduoduo to go to the interface

See all articles