Custom events and jquery plug-ins
The implementation of the query plug-in is greatly affected by the mechanism of custom events. Similarly, custom events are also processed with Dom
A good architectural method for coupling between logic snippets of code that generate interactions.
When you want to add a feature snippet to your application, you may often struggle to determine whether to detach it as a plug-in. The idea of custom events can help you with this decoupling and gradually form a reusable library.
For example, let's look at a simple jquery plug-in -- tab. Let ul
List to respond to click events. When a user clicks a list item, add an item named active to this list item
And remove the active class from other list items:
<Ul id = "tabs">
<Li data-tab = "users"> Users </LI>
<Li data-tab = "groups"> groups </LI>
</Ul>
<Div id = "tabscontent">
<Div data-tab = "users">... </div>
<Div data-tab = "groups">... </div>
</Div>
In addition, the ID is tabscontent.
To store the actual content of each tab. According to the currently activated tab
To add or delete the active class. The actual display and hide tabs and content are composed of CSS
To control, our plug-in only processes the active class:
Jquery. FN. Tabs = function (Control ){
VaR element = $ (this );
Control = $ (control );
Element. Find ("Li"). BIND ("click", function (){
// Add or delete an active object from the list
Class
Element. Find ("Li"). removeclass ("active ");
$ (This). addclass ("active ");
// Give tabcontent
Add or delete an active class
VaR tabname = $ (this). ATTR ("data-tab ");
Control. Find ("> [data-Tab]"). removeclass ("active ");
Control. Find ("> [data-tab = '" + tabname + "']"). addclass ("active ");
});
// Activate 1st
Tabs
Element. Find ("Li: First"). addclass ("active ");
// Return this
To enable chained call
Return this;
};
The plug-in is located in jquery
In prototype, you can call it based on the jquery instance:
$ ("Ul # tabs"). tabs ("# tabcontent ");
Does the plug-in seem to have any problems? That's right. We added a click to all the list items.
Event Callback. This is 1st error. We can use the delegate () mentioned above ()
To optimize the code. Similarly, the implementation of Click Event Callback is very bloated and it is difficult to see what happened at a glance. In addition, if another developer wants to extend the plug-in, he may rewrite it.
Let's take a look at how to use custom events to make the code more clean. When you click the tab, a change. tabs is triggered.
Event, and bind several callback methods to modify the active class properly:
Jquery. FN. Tabs = function (Control ){
VaR element = $ (this );
Control = $ (control );
Element. Delegate ("Li", "click", function (){
// Traverse the tab name
VaR tabname = $ (this). ATTR ("data-tab ");
// Trigger a custom event when you click the tab
Element. Trigger ("change. Tabs", tabname );
});
// Bind to a Custom Event
Element. BIND ("change. Tabs", function (E, tabname ){
Element. Find ("Li"). removeclass ("active ");
Element. Find ("> [data-tab = '" + tabname + "']"). addclass ("active ");
});
Element. BIND ("change. Tabs", function (E, tabname ){
Control. Find ("> [data-Tab]"). removeclass ("active ");
Control. Find ("> [data-tab = '" + tabname + "']"). addclass ("active ");
});
// Activate 1st
Tabs
VaR firstname = element. Find ("Li: First"). ATTR ("data-tab ");
Element. Trigger ("change. Tabs", firstname );
Return this;
};
We can see that the use of Custom Event Callback can make the code more clean. This also means that the tab status switch callback is separated from each other, which makes the plug-in code more scalable. For example, you can directly change the status of the tab in the program. You only need to trigger change. tabs in the observed list.
Event:
$ ("# Tabs"). Trigger ("change. Tabs", "users ");
Similarly, we can switch the tab action and the hash of the window
Then, you can use the back button of the browser:
$ ("# Tabs"). BIND ("change. Tabs", function (E, tabname ){
Window. Location. Hash = tabname;
});
$ (Window). BIND ("hashchange", function (){
VaR tabname = Window. Location. Hash. Slice (1 );
$ ("# Tabs"). Trigger ("change. Tabs", tabname );
});
The use of custom events actually gives other developers a lot of space to expand our work results.
This article is excerpted from the book "javascript web rich application development based on MVC ".
Maccaw,.
Li Jing, translated by Zhang Sanji
This book teaches you how to build advanced rich applications. Many excellent tools and best practices are urgently needed by many programmers and engineers at work.