Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created February 15, 2010 22:29
Show Gist options
  • Save lsmith/305056 to your computer and use it in GitHub Desktop.
Save lsmith/305056 to your computer and use it in GitHub Desktop.
var inputs = Y.all( 'input' );
inputs.on( 'focus', function ( e ) {
// NodeList subscriptions use the NodeList as 'this'
this === inputs; // true
// because you can always get to the node from the event's currentTarget,
// but can't get to the NodeList unless you've prefetched it like in this ex
e.currentTarget.get( 'tagName' ) === 'INPUT'; // true
} );
// So here, for example, the NodeList is not stored, and so otherwise would be
// unavailable to the callback.
Y.all( 'input' ).on( 'focus', function ( e ) {
???.odd().addClass( 'stripe' );
} );
// Y.all( x ).on( y, fn ) is essentially equivalent to
// Y.all( x ).each( function ( item ) { item.on( y, fn, Y.all( x ) ); } );
// All that said, prefer event delegation for subscribing to multiple elements
// with the same handler.
Y.one( 'doc' ).delegate( 'focus', function ( e ) {
// Context is corrected to the input
this.get( 'nodeName' ) === 'INPUT'; // true
// As is the event's currentTarget
e.currentTarget.get( 'nodeName' ) === 'INPUT'; // true
} );
// Incidentally, focus and blur don't bubble in all browsers, but YUI 3 corrects
// that if you include the event-focus or event (rollup) module.
YUI().use( 'node', 'event', function ( Y ) { /* go crazy */ } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment