Web Development with jQuery®

(Elliott) #1

(^80) ❘ CHAPTER 3 EVENTS
The preceding example adds two new buttons to the window, which you can see as shown in
Figure 3-3.


FIGURE 3-3


In Example 3-4, the click event handler is applied using the applyEventHandler method, which uses
jQuery’s on() method with a named event handler click.finder. The event is specifi ed as usual, and
then a dot is inserted, and any name you like is added after the dot (any name following the same
naming conventions as class or id names). You can also use multiple names if you like; in this exam-
ple, you could have also used click.finder.selection.

The example also adds a button and a method to remove the click event handler. The off() method
is called with the same event and event name as was used in the call to the on() method. $('div.
finderFiles').off('click.finder') completely removes the event handler.

Creating Custom Events


Custom events are created using the same methods that you use to attach standard events: on(),
off(), and trigger(). The only difference is that custom events require custom names. Custom
names should simply require whatever you intend the event to provide.

Following are some examples of custom events from the context of a fi le manager application:


➤ (^) An upload event can be created and used to execute a callback handler after a fi le upload has
been completed.
➤ (^) A folderUpdate event can be created and used to execute a callback handler when the fi les
and folders displayed in the fi le manager are changed.
➤ (^) A fileRename event can be created and used to execute a callback handler when a fi le is
renamed.
http://www.it-ebooks.info

Free download pdf