Skip to content

Commit eddb7fc

Browse files
committed
fix indentation and add dmethvin github to Event Extensions article
1 parent 3ee1801 commit eddb7fc

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

content/events/event-extensions.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ chapter : events
33
section : 7
44
title : jQuery Event Extensions
55
attribution: Dave Methvin
6+
github: dmethvin
67
---
78
jQuery offers several ways to extend its event system to provide custom functionality when events are attached to elements. Internally in jQuery, these extensions are primarily used to ensure that standard events such as `submit` and `change` behave consistently across browsers. However, they can also be used to define new events with custom behavior.
89

@@ -43,10 +44,10 @@ To add a property name to this list, use `jQuery.event.props.push("newPropertyNa
4344
The fixHooks interface provides a per-event-type way to extend or normalize the event object that jQuery creates when it processes a *native* browser event. A fixHooks entry is an object that has two properties, each being optional:
4445

4546
props: Array
46-
: Strings representing properties that should be copied from the browser's event object to the jQuery event object. If omitted, no additional properties are copied beyond the standard ones that jQuery copies and normalizes (e.g., event.target and event.relatedTarget).
47+
: Strings representing properties that should be copied from the browser's event object to the jQuery event object. If omitted, no additional properties are copied beyond the standard ones that jQuery copies and normalizes (e.g., event.target and event.relatedTarget).
4748

4849
filter: Function( event, originalEvent )
49-
: jQuery calls this function after it constructs the jQuery.Event object, copies standard properties from `jQuery.event.props`, and copies the fixHooks-specific props (if any) specified above. The function can create new properties on the event object or modify existing ones. The second argument is the browser's native event object, which is also availble in `event.originalEvent`.
50+
: jQuery calls this function after it constructs the jQuery.Event object, copies standard properties from `jQuery.event.props`, and copies the fixHooks-specific props (if any) specified above. The function can create new properties on the event object or modify existing ones. The second argument is the browser's native event object, which is also availble in `event.originalEvent`.
5051

5152
Note that for all events, the browser's native event object is available in `event.originalEvent`; if the jQuery event handler examines the properties there instead of jQuery's normalized `event` object, there is no need to create a fixHooks entry to copy or modify the properties.
5253

@@ -58,7 +59,7 @@ For example, to set a hook for the "drop" event that copies the "dataTransfer" p
5859
Since fixHooks are an advanced feature and rarely used externally, we have not added extra code and interfaces to deal with conflict resolution. If there is a chance that some other code may be assigning fixHooks to the same events, the code should check for an existing hook and take appropriate measures. A simple solution might look like this:
5960

6061
<javascript>if ( jQuery.event.fixHooks.drop ) {
61-
throw new Error("Someone else took the jQuery.event.fixHooks.drop hook!");
62+
throw new Error("Someone else took the jQuery.event.fixHooks.drop hook!");
6263
}
6364
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
6465
</javascript>
@@ -67,13 +68,13 @@ When there are known cases of different plugins wanting to attach to the drop ho
6768

6869
<javascript>var existingHook = jQuery.event.fixHooks.drop;
6970
if ( !existingHook ) {
70-
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
71+
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
7172
} else {
72-
if ( existingHook.props ) {
73-
existingHook.props.push( "dataTransfer" );
74-
} else {
75-
existingHook.props = [ "dataTransfer" ];
76-
}
73+
if ( existingHook.props ) {
74+
existingHook.props.push( "dataTransfer" );
75+
} else {
76+
existingHook.props = [ "dataTransfer" ];
77+
}
7778
}
7879
</javascript>
7980

@@ -93,30 +94,30 @@ When defined, these string properties specify that a special event should be han
9394
The behavior of these properties is easiest to see with an example. Assume a special event defined as follows:
9495

9596
<javascript>jQuery.event.special.pushy = {
96-
bindType: "click",
97-
delegateType: "click"
97+
bindType: "click",
98+
delegateType: "click"
9899
};
99100
</javascript>
100101

101102
When these properties are defined, the following behavior occurs in the jQuery event system:
102103

103-
* Event handlers for the "pushy" event are actually attached to "click" -- both directly bound and delegated events.
104-
* Special event hooks for "click" are called if they exist, *except* the `handle` hook for "pushy" is called when an event is delivered if one exists.
105-
* Event handlers for "pushy" must be removed using the "pushy" event name, and are unaffected if "click" events are removed from the same elements.
104+
* Event handlers for the "pushy" event are actually attached to "click" -- both directly bound and delegated events.
105+
* Special event hooks for "click" are called if they exist, *except* the `handle` hook for "pushy" is called when an event is delivered if one exists.
106+
* Event handlers for "pushy" must be removed using the "pushy" event name, and are unaffected if "click" events are removed from the same elements.
106107

107108

108109
So given the special event above, this code shows that a pushy isn't removed by removing clicks. That might be an effective way to defend against an ill-behaved plugin that didn't namespace its removal of click events, for example:
109110

110111
<javascript>var $p = $("p");
111112
$p.on("click", function( e ) {
112-
$("body").append("I am a " + e.type + "! "));
113+
$("body").append("I am a " + e.type + "!"));
113114
});
114115
$p.on("pushy", function( e ) {
115-
$("body").append("I am pushy but still a " + e.type + "! ");
116+
$("body").append("I am pushy but still a " + e.type + "!");
116117
});
117-
$p.trigger("click"); // triggers both handlers
118+
$p.trigger("click"); // triggers both handlers
118119
$p.off("click");
119-
$p.trigger("click"); // still triggers "pushy"
120+
$p.trigger("click"); // still triggers "pushy"
120121
$p.off("pushy");
121122
</javascript>
122123

@@ -130,22 +131,22 @@ Many of the special event hook functions below are passed a `handleObj` object t
130131
</javascript>
131132

132133
type: String
133-
: The type of event, such as `"click"`. When special event mapping is used via bindType or delegateType, this will be the mapped type.
134+
: The type of event, such as `"click"`. When special event mapping is used via bindType or delegateType, this will be the mapped type.
134135

135136
origType: String
136-
: The original type name (in this case, `"click"`) regardless of whether it was mapped via bindType or delegateType. So when a "pushy" event is mapped to "click" its origType would be "pushy". See the examples in those special event properties above for more detail.
137+
: The original type name (in this case, `"click"`) regardless of whether it was mapped via bindType or delegateType. So when a "pushy" event is mapped to "click" its origType would be "pushy". See the examples in those special event properties above for more detail.
137138

138139
namespace: String
139-
: Namespace(s), if any, provided when the event was attached, such as `"myPlugin"`. When multiple namespaces are given, they are separated by periods and sorted in ascending alphabetical order. If no namespaces are provided, this property is an empty string.
140+
: Namespace(s), if any, provided when the event was attached, such as `"myPlugin"`. When multiple namespaces are given, they are separated by periods and sorted in ascending alphabetical order. If no namespaces are provided, this property is an empty string.
140141

141142
selector: String
142-
: For delegated events, this is the selector used to filter descendant elements and determine if the handler should be called. In the example it is `"button"`. For directly bound events, this property is `null`.
143+
: For delegated events, this is the selector used to filter descendant elements and determine if the handler should be called. In the example it is `"button"`. For directly bound events, this property is `null`.
143144

144145
data: Object
145-
: The data, if any, passed to jQuery during event binding, e.g., `{myData: 42}`. If the data argument was omitted or `undefined`, this property is `undefined` as well.
146+
: The data, if any, passed to jQuery during event binding, e.g., `{myData: 42}`. If the data argument was omitted or `undefined`, this property is `undefined` as well.
146147

147148
handler: function( event: jQuery.Event )
148-
: Event handler function passed to jQuery during event binding; in the example it is a reference to `myHandler`. If `false` was passed during event binding, the handler refers to a single shared function that simply returns `false`.
149+
: Event handler function passed to jQuery during event binding; in the example it is a reference to `myHandler`. If `false` was passed during event binding, the handler refers to a single shared function that simply returns `false`.
149150

150151
#### setup: function( data: Object, namespaces, eventHandle: function )
151152

@@ -191,26 +192,26 @@ This `multiclick` special event maps itself into a standard click event, but use
191192
The hook stores the current click count in the data object, so multiclick handlers on different elements don't interfere with each other. It changes the event type to the original "multiclick" type before calling the handler and restores it to the mapped "click" type before returning:
192193

193194
<javascript>jQuery.event.special.multiclick = {
194-
delegateType: "click",
195-
bindType: "click",
196-
handle: function( event ) {
197-
var handleObj = event.handleObj,
198-
targetData = jQuery.data( event.target ),
199-
ret;
200-
201-
// If a multiple of the click count, run the handler
202-
targetData.clicks = ( targetData.clicks || 0 ) + 1;
203-
if ( targetData.clicks % event.data === 0 ) {
204-
event.type = handleObj.origType;
205-
ret = handleObj.handler.apply( this, arguments );
206-
event.type = handleObj.type;
207-
return ret;
208-
}
209-
}
195+
delegateType: "click",
196+
bindType: "click",
197+
handle: function( event ) {
198+
var handleObj = event.handleObj,
199+
targetData = jQuery.data( event.target ),
200+
ret;
201+
202+
// If a multiple of the click count, run the handler
203+
targetData.clicks = ( targetData.clicks || 0 ) + 1;
204+
if ( targetData.clicks % event.data === 0 ) {
205+
event.type = handleObj.origType;
206+
ret = handleObj.handler.apply( this, arguments );
207+
event.type = handleObj.type;
208+
return ret;
209+
}
210+
}
210211
};
211212

212213
// Sample usage
213214
$("p").on("multiclick", { clicks: 3 }, function(e){
214-
alert("clicked 3 times");
215+
alert("clicked 3 times");
215216
});
216-
</javascript>
217+
</javascript>

0 commit comments

Comments
 (0)