SlideShare una empresa de Scribd logo
1 de 17
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/

Más contenido relacionado

La actualidad más candente

Big Data for each one of us
Big Data for each one of usBig Data for each one of us
Big Data for each one of us
OSCON Byrum
 
Jquery introduce
Jquery introduceJquery introduce
Jquery introduce
Major Ye
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
dmethvin
 
Magento Dependency Injection
Magento Dependency InjectionMagento Dependency Injection
Magento Dependency Injection
Anton Kril
 
Delivering a Responsive UI
Delivering a Responsive UIDelivering a Responsive UI
Delivering a Responsive UI
Rebecca Murphey
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
adamlogic
 

La actualidad más candente (20)

Matters of State
Matters of StateMatters of State
Matters of State
 
Big Data for each one of us
Big Data for each one of usBig Data for each one of us
Big Data for each one of us
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événements
 
Event handling using jQuery
Event handling using jQueryEvent handling using jQuery
Event handling using jQuery
 
Jquery introduce
Jquery introduceJquery introduce
Jquery introduce
 
Bacbkone js
Bacbkone jsBacbkone js
Bacbkone js
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
 
Magento Dependency Injection
Magento Dependency InjectionMagento Dependency Injection
Magento Dependency Injection
 
Client Web
Client WebClient Web
Client Web
 
What Would You Do? With John Quinones
What Would You Do? With John QuinonesWhat Would You Do? With John Quinones
What Would You Do? With John Quinones
 
Functionality Focused Code Organization
Functionality Focused Code OrganizationFunctionality Focused Code Organization
Functionality Focused Code Organization
 
Delivering a Responsive UI
Delivering a Responsive UIDelivering a Responsive UI
Delivering a Responsive UI
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
 
Beyond the DOM: Sane Structure for JS Apps
Beyond the DOM: Sane Structure for JS AppsBeyond the DOM: Sane Structure for JS Apps
Beyond the DOM: Sane Structure for JS Apps
 
Events
EventsEvents
Events
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
BVJS
BVJSBVJS
BVJS
 
Evented Javascript
Evented JavascriptEvented Javascript
Evented Javascript
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
International News | World News
International News | World NewsInternational News | World News
International News | World News
 

Destacado

Soc 002 Sanford Jessica
Soc 002 Sanford JessicaSoc 002 Sanford Jessica
Soc 002 Sanford Jessica
sanfordj1
 
la città di dio
la città di diola città di dio
la città di dio
beaotti
 
Soc 002 Sanford Jessica
Soc 002 Sanford JessicaSoc 002 Sanford Jessica
Soc 002 Sanford Jessica
sanfordj1
 
Skype Day In Taiwan(Tommy)
Skype  Day In  Taiwan(Tommy)Skype  Day In  Taiwan(Tommy)
Skype Day In Taiwan(Tommy)
tommy.sheu
 
L A C I T T A’ D I D I O
L A  C I T T A’  D I  D I OL A  C I T T A’  D I  D I O
L A C I T T A’ D I D I O
beaotti
 
Alterbridgepresentation
AlterbridgepresentationAlterbridgepresentation
Alterbridgepresentation
StixFactor
 
G I O R D A N O B R U N O
G I O R D A N O  B R U N OG I O R D A N O  B R U N O
G I O R D A N O B R U N O
beaotti
 
Special events in public relations
Special events in public relations Special events in public relations
Special events in public relations
Rohit Kumar
 

Destacado (19)

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

Similar a Special Events: Beyond Custom Events

international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
Robert Nyman
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, MoscowJavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Robert Nyman
 
[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx
gerardkortney
 

Similar a Special Events: Beyond Custom Events (20)

international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
Clean Javascript
Clean JavascriptClean Javascript
Clean Javascript
 
jQuery: Events, Animation, Ajax
jQuery: Events, Animation, AjaxjQuery: Events, Animation, Ajax
jQuery: Events, Animation, Ajax
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
 
Building Large jQuery Applications
Building Large jQuery ApplicationsBuilding Large jQuery Applications
Building Large jQuery Applications
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
 
G* on GAE/J 挑戦編
G* on GAE/J 挑戦編G* on GAE/J 挑戦編
G* on GAE/J 挑戦編
 
State of jQuery and Drupal
State of jQuery and DrupalState of jQuery and Drupal
State of jQuery and Drupal
 
Javascript essential-pattern
Javascript essential-patternJavascript essential-pattern
Javascript essential-pattern
 
jQuery
jQueryjQuery
jQuery
 
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, MoscowJavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
 
amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5
 
Unit3.pptx
Unit3.pptxUnit3.pptx
Unit3.pptx
 
[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
Events - Part 2
Events - Part 2Events - Part 2
Events - Part 2
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
Building iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" DominoBuilding iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" Domino
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
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/

Notas del editor

  1. 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