SlideShare a Scribd company logo
Special Events
 Beyond Custom Events
Brandon Aaron
 jQuery Core Contributor
Senior Developer at Simpli.fi



    http://brandonaaron.net/
http://twitter.com/brandonaaron
Special Events
 Beyond Custom Events
jQuery Events

• Passing extra data to events

     $('div')
         .bind('click', { test: true }, function(event, data) {
              var bindData = event.data && event.data.test,
                  triggerData = data && data.test;
             console.log(bindData, triggerData);
         })
         .trigger('click', { test: true });
jQuery Events

• Event namespaces provide easy unbinding

            $('input')
              .bind('focus.myplugin', fn)
              .bind('blur.myplugin', fn);

            $('input').unbind('.myplugin');
jQuery Events

• Any event type

    $('div')
        .bind('myevent', { test: true }, function(event, data) {
             var bindData = event.data && event.data.test,
                 triggerData = data && data.test;
            console.log(event.type, bindData, triggerData);
        })
        .trigger('myevent', { test: true });
Why so special?

• Override existing events
• Normalize existing events
• Enhance existing events
• Create new events
• Use just like other events
setup and teardown
• Runs once per an event type per an element
• return false; to tell jQuery to handle the
  event binding instead
• Available since 1.2.2
             jQuery.event.special.tripleclick = {
                 setup: function(data, namespaces) {
                     var elem = this;
                 },

                  teardown: function(namespaces) {
                      var elem = this;
                  }
             };



 http://brandonaaron.net/blog/2009/03/26/special-events
jQuery.event.special.tripleclick = {
    setup: function(data, namespaces) {
        var elem = this, $elem = jQuery(elem);
        $elem.bind('click', jQuery.event.special.tripleclick.handler);
    },

     teardown: function(namespaces) {
         var elem = this, $elem = jQuery(elem);
         $elem.unbind('click', jQuery.event.special.tripleclick.handler);
     },

     handler: function(event) {
         var elem = this, $elem = jQuery(elem), clicks = $elem.data('clicks') || 0;
         clicks += 1;
         if ( clicks === 3 ) {
             clicks = 0;
             // set event type to "tripleclick"
             event.type = "tripleclick";
             // let jQuery handle the triggering of "tripleclick" event handlers
             jQuery.event.handle.apply(this, arguments)
         }
         $elem.data('clicks', clicks);
     }
};
tripleclick usage


     jQuery('div').bind('tripleclick', function(event) {
         alert('triple clicked');
     });




http://brandonaaron.net/examples/special-events/1
add and remove

     • Runs once for every event handler added
     • Has the ability to change the event handler,
         data, and namespaces
     • Available since 1.4.2          (technically since 1.4 but changed in 1.4.2)




http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks
http://brandonaaron.net/blog/2010/02/25/special-events-the-changes-in-1-4-2
jQuery.event.special.multiclick = {
    add: function( details ) {
        var handler   = details.handler,
            data      = details.data,
            namespace = details.namespace;
    },

     remove: function( details ) {
         var handler   = details.handler,
             data      = details.data,
             namespace = details.namespace;
     }
};
jQuery.event.special.multiclick = {
    add: function( details ) {
        var handler   = details.handler,
            data      = details.data,
            threshold = data && data.threshold || 1,
            clicks    = 0;

          // replace the handler
          details.handler = function(event) {
              // increase number of clicks
              clicks += 1;
              if ( clicks === threshold ) {
                // required number of clicks reached, reset
                clicks = 0;
                // call the actual supplied handler
                handler.apply( this, arguments );
              }
          };
     },

     setup: function( data, namespaces ) {
         jQuery( this ).bind( "click", jQuery.event.special.multiclick.handler );
     },

     teardown: function( namespaces ) {
         jQuery( this ).unbind( "click", jQuery.event.special.multiclick.handler );
     },

     handler: function( event ) {
         // set correct event type
         event.type = "multiclick";
         // trigger multiclick handlers
         jQuery.event.handle.apply( this, arguments );
     }
};
multiclick usage

          $('div')
              .bind("multiclick",    { threshold: 5 }, function( event ) {
                   alert( "Clicked   5 times" );
              })
              .bind("multiclick",    { threshold: 3 }, function( event ) {
                   alert( "Clicked   3 times" );
              });




http://brandonaaron.net/examples/special-events-the-changes-in-1-4-2/1
one more example

               jQuery.event.special.click = {
                   setup: function() {
                       jQuery( this ).css( 'cursor', 'pointer' );
                       return false;
                   },

                    teardown: fuction() {
                        jQuery( this ).css( 'cursor', '' );
                        return false;
                    }
               };




http://brandonaaron.net/blog/2009/06/17/automating-with-special-events
one more hook
• A default action for custom events
• Just like native default actions
• event.preventDefault() does what it says
             jQuery.event.special.destroy = {
                 _default: function(event) {
                     jQuery(event.target).remove();
                 }
             };




http://benalman.com/news/2010/03/jquery-special-events/
links
http://brandonaaron.net/

http://twitter.com/brandonaaron

http://brandonaaron.net/blog/2009/03/26/special-events

http://brandonaaron.net/examples/special-events/1

http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks

http://brandonaaron.net/blog/2010/02/25/special-events-the-changes-in-1-4-2

http://brandonaaron.net/examples/special-events-the-changes-in-1-4-2/1

http://benalman.com/news/2010/03/jquery-special-events/

More Related Content

KEY
Special Events
PDF
jQuery secrets
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
PDF
How Kris Writes Symfony Apps
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
PDF
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
PDF
How kris-writes-symfony-apps-london
Special Events
jQuery secrets
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
How Kris Writes Symfony Apps
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
How kris-writes-symfony-apps-london

What's hot (20)

PDF
Matters of State
PPTX
Big Data for each one of us
PDF
Javascript #8 : événements
ODP
Event handling using jQuery
PPTX
Jquery introduce
PPTX
PPT
jQuery 1.7 Events
PPTX
Magento Dependency Injection
PPTX
Client Web
PDF
What Would You Do? With John Quinones
PDF
Functionality Focused Code Organization
PDF
Delivering a Responsive UI
PDF
jQuery and Rails, Sitting in a Tree
PDF
Beyond the DOM: Sane Structure for JS Apps
KEY
Events
PPT
JavaScript: Events Handling
PPTX
Evented Javascript
PDF
How Kris Writes Symfony Apps
PDF
International News | World News
Matters of State
Big Data for each one of us
Javascript #8 : événements
Event handling using jQuery
Jquery introduce
jQuery 1.7 Events
Magento Dependency Injection
Client Web
What Would You Do? With John Quinones
Functionality Focused Code Organization
Delivering a Responsive UI
jQuery and Rails, Sitting in a Tree
Beyond the DOM: Sane Structure for JS Apps
Events
JavaScript: Events Handling
Evented Javascript
How Kris Writes Symfony Apps
International News | World News
Ad

Viewers also liked (19)

PPT
Soc 002 Sanford Jessica
PPT
la città di dio
PPT
Soc 002 Sanford Jessica
PPT
Presentazione Méntore alla Fiera di Modena 8 ottobre 2008
PPTX
Sistemas automáticos
PPT
Skype Day In Taiwan(Tommy)
PPT
Strategic Human Resources, Llc
PPT
Francesco Bacone Bea E Otti
PPT
Mentore 2.0 V2 Beta2
PPT
Strategic Human Resources, Llc
PPT
L A C I T T A’ D I D I O
PPT
Alterbridgepresentation
KEY
Intro To Sammy
PPTX
Sistemas mecánicos
PPT
Strategic Human Resources, Llc
PPTX
Sensores
PPT
G I O R D A N O B R U N O
PPTX
Special events in public relations
PPTX
Mentoring
Soc 002 Sanford Jessica
la città di dio
Soc 002 Sanford Jessica
Presentazione Méntore alla Fiera di Modena 8 ottobre 2008
Sistemas automáticos
Skype Day In Taiwan(Tommy)
Strategic Human Resources, Llc
Francesco Bacone Bea E Otti
Mentore 2.0 V2 Beta2
Strategic Human Resources, Llc
L A C I T T A’ D I D I O
Alterbridgepresentation
Intro To Sammy
Sistemas mecánicos
Strategic Human Resources, Llc
Sensores
G I O R D A N O B R U N O
Special events in public relations
Mentoring
Ad

Similar to Special Events: Beyond Custom Events (20)

PDF
international PHP2011_Bastian Feder_jQuery's Secrets
PDF
jQuery secrets
PDF
Clean Javascript
PDF
jQuery: Events, Animation, Ajax
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
PDF
Building Large jQuery Applications
PDF
Javascript Frameworks for Joomla
PDF
G* on GAE/J 挑戦編
PDF
State of jQuery and Drupal
PDF
Javascript essential-pattern
PDF
PDF
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
KEY
amsterdamjs - jQuery 1.5
PPTX
Unit3.pptx
DOCX
[removed] $file, removeRemove}, list #su.docx
PDF
WordPress Realtime - WordCamp São Paulo 2015
PPTX
Events - Part 2
PDF
Writing Maintainable JavaScript
PDF
Building iPhone Web Apps using "classic" Domino
PDF
DOM Scripting Toolkit - jQuery
international PHP2011_Bastian Feder_jQuery's Secrets
jQuery secrets
Clean Javascript
jQuery: Events, Animation, Ajax
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
Building Large jQuery Applications
Javascript Frameworks for Joomla
G* on GAE/J 挑戦編
State of jQuery and Drupal
Javascript essential-pattern
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
amsterdamjs - jQuery 1.5
Unit3.pptx
[removed] $file, removeRemove}, list #su.docx
WordPress Realtime - WordCamp São Paulo 2015
Events - Part 2
Writing Maintainable JavaScript
Building iPhone Web Apps using "classic" Domino
DOM Scripting Toolkit - jQuery

Special Events: Beyond Custom Events

  • 1. Special Events Beyond Custom Events
  • 2. Brandon Aaron jQuery Core Contributor Senior Developer at Simpli.fi http://brandonaaron.net/ http://twitter.com/brandonaaron
  • 3. Special Events Beyond Custom Events
  • 4. jQuery Events • Passing extra data to events $('div') .bind('click', { test: true }, function(event, data) { var bindData = event.data && event.data.test, triggerData = data && data.test; console.log(bindData, triggerData); }) .trigger('click', { test: true });
  • 5. jQuery Events • Event namespaces provide easy unbinding $('input') .bind('focus.myplugin', fn) .bind('blur.myplugin', fn); $('input').unbind('.myplugin');
  • 6. jQuery Events • Any event type $('div') .bind('myevent', { test: true }, function(event, data) { var bindData = event.data && event.data.test, triggerData = data && data.test; console.log(event.type, bindData, triggerData); }) .trigger('myevent', { test: true });
  • 7. Why so special? • Override existing events • Normalize existing events • Enhance existing events • Create new events • Use just like other events
  • 8. setup and teardown • Runs once per an event type per an element • return false; to tell jQuery to handle the event binding instead • Available since 1.2.2 jQuery.event.special.tripleclick = { setup: function(data, namespaces) { var elem = this; }, teardown: function(namespaces) { var elem = this; } }; http://brandonaaron.net/blog/2009/03/26/special-events
  • 9. jQuery.event.special.tripleclick = { setup: function(data, namespaces) { var elem = this, $elem = jQuery(elem); $elem.bind('click', jQuery.event.special.tripleclick.handler); }, teardown: function(namespaces) { var elem = this, $elem = jQuery(elem); $elem.unbind('click', jQuery.event.special.tripleclick.handler); }, handler: function(event) { var elem = this, $elem = jQuery(elem), clicks = $elem.data('clicks') || 0; clicks += 1; if ( clicks === 3 ) { clicks = 0; // set event type to "tripleclick" event.type = "tripleclick"; // let jQuery handle the triggering of "tripleclick" event handlers jQuery.event.handle.apply(this, arguments) } $elem.data('clicks', clicks); } };
  • 10. tripleclick usage jQuery('div').bind('tripleclick', function(event) { alert('triple clicked'); }); http://brandonaaron.net/examples/special-events/1
  • 11. add and remove • Runs once for every event handler added • Has the ability to change the event handler, data, and namespaces • Available since 1.4.2 (technically since 1.4 but changed in 1.4.2) http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks http://brandonaaron.net/blog/2010/02/25/special-events-the-changes-in-1-4-2
  • 12. jQuery.event.special.multiclick = { add: function( details ) { var handler = details.handler, data = details.data, namespace = details.namespace; }, remove: function( details ) { var handler = details.handler, data = details.data, namespace = details.namespace; } };
  • 13. jQuery.event.special.multiclick = { add: function( details ) { var handler = details.handler, data = details.data, threshold = data && data.threshold || 1, clicks = 0; // replace the handler details.handler = function(event) { // increase number of clicks clicks += 1; if ( clicks === threshold ) { // required number of clicks reached, reset clicks = 0; // call the actual supplied handler handler.apply( this, arguments ); } }; }, setup: function( data, namespaces ) { jQuery( this ).bind( "click", jQuery.event.special.multiclick.handler ); }, teardown: function( namespaces ) { jQuery( this ).unbind( "click", jQuery.event.special.multiclick.handler ); }, handler: function( event ) { // set correct event type event.type = "multiclick"; // trigger multiclick handlers jQuery.event.handle.apply( this, arguments ); } };
  • 14. multiclick usage $('div') .bind("multiclick", { threshold: 5 }, function( event ) { alert( "Clicked 5 times" ); }) .bind("multiclick", { threshold: 3 }, function( event ) { alert( "Clicked 3 times" ); }); http://brandonaaron.net/examples/special-events-the-changes-in-1-4-2/1
  • 15. one more example jQuery.event.special.click = { setup: function() { jQuery( this ).css( 'cursor', 'pointer' ); return false; }, teardown: fuction() { jQuery( this ).css( 'cursor', '' ); return false; } }; http://brandonaaron.net/blog/2009/06/17/automating-with-special-events
  • 16. one more hook • A default action for custom events • Just like native default actions • event.preventDefault() does what it says jQuery.event.special.destroy = { _default: function(event) { jQuery(event.target).remove(); } }; http://benalman.com/news/2010/03/jquery-special-events/

Editor's Notes

  • #3: Contributing since Aug 2006 Contribute number of plugins like Live Query and bgiframe Maintain a blog to share knowledge Nokia in March as a senior technologist to explore the mobile web Nokia investing significantly in web technologies