Namespaces for binding events in jquery _jquery

Source: Internet
Author: User
Tags anonymous
Before I saw this article, I never knew that bind could have a namespace. In fact, after reading this article, I went through the manual and found a little comment. But the manual is only a word to take the past. Not too much to delve into, perhaps he thought the namespace this thing is very simple, does not need to explain more?

Read the manual first, because the bind method has three parameters (TYPE,[DATA],FN), so the manual describes:

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 a few 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 parameter of type is legitimate, and if a string is not a 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 manually triggered in other code by using the. trigger () or. Triggerhandler ().

If the string of the type parameter contains a point (.) Character, this event is considered a namespace. This dot character is used to separate the event from his namespace. For example, if you execute. Bind (' Click.name ', handler), then the 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 the handler functions that are bound to the event above are triggered. If multiple event handlers are registered, they are always triggered in the order in which they are bound. After all the bound event handlers have been executed, the event continues to float along the normal event bubbling path.



The Bind/unbind method of jquery should be said to be simple, and most of the time may not be used, instead of directly using the Click/keydown type of event-style methods to do event binding operations.

However, suppose that the binding of different Click event processing logic is required at run time based on the results of user interaction, so that an event can theoretically be bind/unbind for countless times. But I also hope that the unbind will only be tied up in the processing logic to release rather than all other places have the possibility of additional the same event binding logic.

At this point, if you use. Click ()/. Bind (' click ') plus. Unbind (' click ') to repeat the binding, the Unbind will be all bound to the elements of the Click processing logic, potentially affecting the other third party of the element behavior. Of course, if a function variable is shown in bind, it is possible to provide function as the second parameter in unbind to specify only one of the processing logic, but it is likely that a variety of anonymous function bindings will be encountered in the actual application unbind.

For this problem, the jquery solution is to use event-bound namespaces. That is, after the event name, Add. Something to differentiate oneself this part of the logical scope of behavior.

For example, with. Bind (' Click.mycustomroutine ', function () {...}); The same is to bind the anonymous function to the click event (you can bind different behavior methods in your own namespace multiple times), and when Unbind, use the. Unbind (' Click.mycustomroutine ') to release all the bindings. Mycustomroutine The Click event of the namespace without releasing other event behaviors that are bound by. Bind (' click ') or another namespace.

Also, using command space allows you to unbind all custom event bindings under this namespace at once, passing. Unbind ('. Mycustomroutine ').

It should be noted that the jquery namespace does not support multiple levels of space. Because in jquery, if you use. Unbind (' Click.myCustomRoutine.myCustomSubone '), the namespace is lifted by the Mycustomroutine and Mycustomsubone, respectively. All click events under the two parallel namespaces, not the Mycustomsubone subspace under Mycustomroutine.

The Bind/unbind method of jquery should be said to be simple, and most of the time may not be used, instead of directly using the Click/keydown type of event-style methods to do event binding operations.

However, suppose that the binding of different Click event processing logic is required at run time based on the results of user interaction, so that an event can theoretically be bind/unbind for countless times. But I also hope that the unbind will only be tied up in the processing logic to release rather than all other places have the possibility of additional the same event binding logic.

At this point, if you use. Click ()/. Bind (' click ') plus. Unbind (' click ') to repeat the binding, the Unbind will be all bound to the elements of the Click processing logic, potentially affecting the other third party of the element behavior. Of course, if a function variable is shown in bind, it is possible to provide function as the second parameter in unbind to specify only one of the processing logic, but it is likely that a variety of anonymous function bindings will be encountered in the actual application unbind.

For this problem, the jquery solution is to use event-bound namespaces. That is, after the event name, Add. Something to differentiate oneself this part of the logical scope of behavior.

For example, with. Bind (' Click.mycustomroutine ', function () {...}); The same is to bind the anonymous function to the click event (you can bind different behavior methods in your own namespace multiple times), and when Unbind, use the. Unbind (' Click.mycustomroutine ') to release all the bindings. Mycustomroutine The Click event of the namespace without releasing other event behaviors that are bound by. Bind (' click ') or another namespace.

Also, using command space allows you to unbind all custom event bindings under this namespace at once, passing. Unbind ('. Mycustomroutine ').

It should be noted that the jquery namespace does not support multiple levels of space. Because in jquery, if you use. Unbind (' Click.myCustomRoutine.myCustomSubone '), the namespace is lifted by the Mycustomroutine and Mycustomsubone, respectively. All click events under the two parallel namespaces, not the Mycustomsubone subspace under Mycustomroutine.

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.