Skip to content

Commit cd22666

Browse files
committed
Event: Warn and fill event aliases
Fixes #230 Closes #243
1 parent dfa308f commit cd22666

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/event.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ jQuery.each( [ "load", "unload", "error" ], function( _, name ) {
7575

7676
} );
7777

78+
jQuery.each( ( "blur focus focusin focusout resize scroll click dblclick " +
79+
"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
80+
"change select submit keydown keypress keyup contextmenu" ).split( " " ),
81+
function( i, name ) {
82+
83+
// Handle event binding
84+
jQuery.fn[ name ] = function( data, fn ) {
85+
migrateWarn( "jQuery.fn." + name + "() event shorthand is deprecated" );
86+
return arguments.length > 0 ?
87+
this.on( name, null, data, fn ) :
88+
this.trigger( name );
89+
};
90+
} );
91+
7892
// Trigger "ready" event only once, on document ready
7993
jQuery( function() {
8094
jQuery( window.document ).triggerHandler( "ready" );
@@ -107,5 +121,9 @@ jQuery.fn.extend( {
107121
return arguments.length === 1 ?
108122
this.off( selector, "**" ) :
109123
this.off( types, selector || "**", fn );
124+
},
125+
hover: function( fnOver, fnOut ) {
126+
migrateWarn( "jQuery.fn.hover() is deprecated" );
127+
return this.on( "mouseenter", fnOver ).on( "mouseleave", fnOut || fnOver );
110128
}
111129
} );

test/event.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,39 @@ QUnit.test( ".delegate() and .undelegate()", function( assert ) {
9494
} );
9595
} );
9696

97+
QUnit.test( "Event aliases", function( assert) {
98+
assert.expect( 14 );
99+
100+
var $div = jQuery( "<div />" );
101+
102+
"scroll click submit keydown".split( " " ).forEach( function( name ) {
103+
expectWarning( "." + name + "()", 1, function() {
104+
$div[ name ]( function( event ) {
105+
assert.equal( event.type, name, name );
106+
$div.off( event );
107+
} )[ name ]();
108+
} );
109+
} );
110+
111+
expectWarning( ".hover() one-arg", 1, function() {
112+
$div.hover( function( event ) {
113+
assert.ok( /mouseenter|mouseleave/.test( event.type ), event.type );
114+
$div.off( event );
115+
} ).trigger( "mouseenter" ).trigger( "mouseleave" );
116+
} );
117+
118+
expectWarning( ".hover() two-arg", 1, function() {
119+
$div.hover(
120+
function( event ) {
121+
assert.equal( "mouseenter", event.type, event.type );
122+
},
123+
function( event ) {
124+
assert.equal( "mouseleave", event.type, event.type );
125+
}
126+
).trigger( "mouseenter" ).trigger( "mouseleave" );
127+
} );
128+
} );
129+
97130
test( "custom ready", function( assert ) {
98131
assert.expect( 2 );
99132

warnings.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,15 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58
211211
**Cause:** Older versions of JavaScript made it difficult to determine if a particular object was a true `Array`, so jQuery provided a cross-browser function to do the work. The browsers supported by jQuery 3.0 all provide `Array.isArray(obj)` for this purpose.
212212

213213
**Solution**: Replace any calls to `jQuery.isArray` with `Array.isArray`.
214+
215+
### JQMIGRATE: jQuery.fn.click() event shorthand is deprecated
216+
217+
**Cause:** The `.on()` and `.trigger()` methods can set an event handler or generate an event for any event type, and should be used instead of the shortcut methods. This message also applies to the other event shorthands, including: blur, focus, focusin, focusout, resize, scroll, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, and contextmenu.
218+
219+
**Solution:** Instead of `.click(fn)` use `.on("click", fn)`. Instead of `.click()` use `.trigger("click")`.
220+
221+
### JQMIGRATE: jQuery.fn.hover() is deprecated
222+
223+
**Cause:** The `.hover()` method is a shorthand for the use of the `mouseover`/`mouseout` events. It is often a poor user interface choice because it does not allow for any small amounts of delay between when the mouse enters or exits an area and when the event fires. This can make it quite difficult to use with UI widgets such as drop-down menus. For more information on the problems of hovering, see the [hoverIntent plugin](http://cherne.net/brian/resources/jquery.hoverIntent.html).
224+
225+
**Solution:** Review uses of `.hover()` to determine if they are appropriate, and consider use of plugins such as `hoverIntent` as an alternative. The direct replacement for `.hover(fn1, fn2)`, is `.on("mouseenter", fn1).on("mouseleave", fn2)`.

0 commit comments

Comments
 (0)