Skip to content

Commit 334cb78

Browse files
committed
Event: Warn and fill event aliases
Fixes #230
1 parent 9e3dfcb commit 334cb78

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,37 @@ QUnit.test( ".delegate() and .undelegate()", function( assert ) {
9494
} );
9595
} );
9696

97+
QUnit.test( "Event aliases", function( assert) {
98+
assert.expect( 16 );
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+
} )[ name ]();
107+
} );
108+
} );
109+
110+
expectWarning( ".hover() one-arg", function() {
111+
$div.hover( function( event ) {
112+
assert.ok( /mouseenter|mouseleave/.test( event.type ), event.type );
113+
} ).trigger( "mouseenter" ).trigger( "mouseleave" );
114+
} );
115+
116+
expectWarning( ".hover() two-arg", function() {
117+
$div.hover(
118+
function( event ) {
119+
assert.equal( "mouseenter", event.type, event.type );
120+
},
121+
function( event ) {
122+
assert.equal( "mouseleave", event.type, event.type );
123+
}
124+
).trigger( "mouseenter" ).trigger( "mouseleave" );
125+
} );
126+
} );
127+
97128
test( "custom ready", function( assert ) {
98129
assert.expect( 2 );
99130

warnings.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,15 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58
199199
**Cause:** The calling code has attempted to attach a `load` event to `window` after the page has already loaded. That means the handler will never run and so is probably not what the caller intended. This can occur when the event attachment is made too late, for example, in a jQuery ready handler. It can also occur when a file is loaded dynamically with jQuery after the page has loaded, for example using the `$.getScript()` method.
200200

201201
**Solution:** If a function `fn` does not actually depend on all page assets being fully loaded, switch to a ready handler `$( fn )` which runs earlier and will aways run `fn` even if the script that contains the code loads long after the page has fully loaded. If `fn` actually does depend on the script being fully loaded, check `document.readyState`. If the value is `"complete"` run the function immediately, otherwise use `$(window).on( "load", fn )`.
202+
203+
### JQMIGRATE: jQuery.fn.click() event shorthand is deprecated
204+
205+
**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.
206+
207+
**Solution:** Instead of `.click(fn)` use `.on("click", fn)`. Instead of `.click()` use `.trigger("click")`.
208+
209+
### JQMIGRATE: jQuery.fn.hover() is deprecated
210+
211+
**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).
212+
213+
**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)