JQuery bind namespace

Source: Internet
Author: User

7834431

Before I saw this article, I didn't know that bind could have a namespace. In fact, after reading this article, I went over the manual and found a little comment. But the handbook is just one sentence away. Not too much to delve into, perhaps he thought the namespace this thing is very simple, there is no need to explain more?

First look at the manual, because the Bind method has three parameters (TYPE,[DATA],FN), so the manual is described:

The. Bind () method is the primary way to attach behavior to a document. All JavaScript event objects, such as focus, mouseover, and resize, can be passed in as type parameters.

jquery also provides some simple ways to bind these standard event types, such as. Click () to simplify. Bind (' click '). There are altogether the following: Blur, Focus, Focusin, focusout, load, resize, scroll, unload, click, DblClick, MouseDown, MouseUp, MouseMove, MOU Seover, Mouseout, MouseEnter, MouseLeave, change, select, Submit, KeyDown, KeyPress, KeyUp, error.

Any string that is a type parameter is legal, and if a string is not the native JavaScript event name, then the event handler is bound to a custom event. These custom events are never triggered by the browser, but can be triggered manually in other code by using. Trigger () or. Triggerhandler ().

If the string of the type parameter contains a dot (.) Character, then this event is considered to be a namespace. This dot character is used to separate the event and his namespace. For example, if you execute. Bind (' Click.name ', handler), then click in the string is the event type, and the string name is the namespace. Namespaces allow us to unbind or trigger certain types of events without triggering other events. Refer to Unbind () for more information.

When an event is passed to an element, all of the handler functions that are bound to the event above are triggered. If multiple event handlers are registered, they are always triggered sequentially in the order in which they are bound. When all the bound event handlers have finished executing, the event continues to float along the normal event bubbling path.



The bind/unbind approach to jquery should be simple to use, and most of the time it may not be used, instead of using event-style methods such as Click/keydown to do event-binding operations.

It is assumed, however, that the binding of different click event-handling logic is required at run time based on the results of user interaction, so that an event can theoretically be bind/unbind for an infinite number of times. But I hope that Unbind will only tie himself up with the processing logic to release the additional same event binding logic that is not possible in all other places.

If you use. Click ()/. Bind (' click ') with. Unbind (' click ') to repeat the binding, the Unbind will be all the click processing logic that binds to the element, potentially affecting the behavior of other third parties of that element. Of course, if a function variable is defined when bind is displayed, the function can be provided as the second parameter in unbind to specify only one of the processing logic, but in practice it is likely to encounter various instances of anonymous function binding.

Rightin this case, the solution to jquery is to use the namespace of event bindings. That is, after the event name, add the. Something to differentiate yourself from this part of the behavior logic scope.

for example, use. bind (' Click.mycustomroutine ', function () {...}), and also bind the anonymous function to the click event (you can bind different behavior methods with your own namespace multiple times), when Unbind . Unbind (' Click.mycustomroutine ') frees all click events bound to the. Mycustomroutine namespace without releasing any other event behavior that is bound by. Bind (' click ') or another namespace.

at the same time, using the command space allows you to unbind all custom event bindings under this namespace at once, via. Unbind ('. Mycustomroutine ').

It is important to note that jquery namespaces do not support multi-level spaces. Because in jquery, if you use. Unbind (' Click.myCustomRoutine.myCustomSubone '), the namespace is Mycustomroutine and Mycustomsubone, respectively. All click events under the two side-by-side namespace, instead of the Mycustomsubone subspace under Mycustomroutine.

JQuery bind namespace

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.