SlideShare a Scribd company logo
jQuery secrets
Bastian Feder              IPC 2011
lapistano@php.net     th
                    10 October 2011
Me, myself & I

 JavaScript since 2002
 Trainer & coach
 Opensource addict
  PHP manual translations
  FluentDOM
  phpDox
  ...
Utilities
Saving the state

 jQuery.data( element, key[, value] )
   Store any kind of information on a DOM
   element
   Circular references avoided
   Low level function use
   $( element ).data( key[, value] ) instead.
Removing the state

 jQuery.removeData( element[, key] )
  Low level function use
  $( element ).removeData( [key] )
  instead.
  Removes all data if no key is passed.
State example

 var $logo = $('#jq-siteLogo');

 $(document).data('logo', $logo);

 $logo.detach();

 $('#content').before($(document).data('logo'));

 $(document).removeData('logo');
Hidden events

 getData[.namespace]
 Emitted, when the content is read.
 setData[.namespace]
 Emitted, when the content is set or reset.
 changeData[.namespace]
 Emitted, when the content has been changed.
 Comes always in hand with the setData event.
Hidden events                    (II)


var foo = $('div');

foo.bind('changeData.data', function( event, key, value ) {

      var data = new Array(value);
      $('#activitystream').trigger('myCustomEvent', data);

});

foo.data('my.data', 'Tux is a mascott.');
Extending for the good

  jQuery.extend([deep], target[, object1][,objectN])

var empty = {};
var defaults = {
    validate : false,
    limit    : {max: 5, min: 1},
    name     : "foo"
};
var options = {
    validate : true,
    limit    : {max:10}
};
var settings = $.extend(true, empty, defaults, options);
User defined properties

 Register of translations
 Used by .attr( )
  jQuery.propFix = {
     for         : "htmlFor",
     class       : "className",
     …
  };

  jQuery.extend(
      jQuery.propFix,
      {uijgh : "ui-jeopardy-gameboard-header"}
  );
DOM attributes

 Applies for every entry in jQuery.attrFn
 (e.g. val, css, text, data, … )


 var tux = $("<div/>", {
     css : {background: '#AAA'},
     text : 'beastie'
 });

 $("div")['css']({background: '#BBB'});
AJAX
Shortcuts       (II)


 jQuery.getJSON(
   url[,data ][,callback(data, textStatus)] )
 jQuery.getScript(
   url[,success(data, textStatus)] )
Global AJAX events

 ajaxStart
 everytime a new request has been fired and no request s running

 ajaxStop
 everytime no request is running anymore

 ajaxComplete
 once for each completed request (success or failure)
Global AJAX events                            (II)


 ajaxError
 once for each failed request

 ajaxSuccess
 once for each successful request

 ajaxSend
 once for each request, but before the request is send
Global AJAX events                   (III)


 jQuery('#loadingIndicator').bind(
    "ajaxStart",
    function() {
       $(this).show();
    }
 );

 jQuery('#loadingIndicator').bind(
    "ajaxStop",
    function() {
       $(this).hide();
    }
 );
Events
Event binding

  .bind(type[, data], handler(event))
      'type' might also be an object


$('.clickable').bind({

  'click'     : function(event) { //callback },
  'mousedown' : function(event) { //callback }

});
Namespaces

$('.clickable').bind('click.namespace', function(e){});

$('.clickable').bind({

  'click.namespace': function(event) { //callback },
  'mousedown.namespace': function(event) { //callback }

});

$('.clickable').unbind('.namespace');
Selfdefined speeding

jQuery.extend(
   jQuery.fx.speeds,
   {
      slow     : 600,
      fast     : 200,
      _default : 400
   },
   { slowmotion: 1000 }
);

$('#clickme').click(function() {
  $('#book').fadeIn('slowmotion');
});
Extending
 jQuery
jQuery plugins

  jQuery.error( message )

jQuery.prototype.error = function( message, errorcode ) {
  var error = [];
  error['msg'] = message;
  error['code'] = errorcode;
  if (typeof console != undefined) {
    console.error(error)
    return;
  }
}
jQuery UI

jQuery.extend('ui.autosuggest.prototype, {
  _search: function( value ) {
    // always save the actual value,
    // not the one passed as an argument
    this.term = this.element
      .addClass( "ui-autocomplete-loading"
      .val();

      this.source( { term: value }, this.response );
});
Questions
@lapistano

lapistano@php.net
Slides'n contact

 Please comment the talk on joind.in
   http://joind.in/3833
 Slides
   http://slideshare.net/lapistano
 Email:
   lapistano@php.net
License

 This set of slides and the source code
 included
   in the download package is licensed under
 the

 Creative Commons Attribution-
 Noncommercial-Share Alike 2.0 Generic
 License

More Related Content

What's hot (20)

PDF
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Darren Mothersele
 
PPTX
Jquery Basics
Umeshwaran V
 
PDF
Functionality Focused Code Organization
Rebecca Murphey
 
PPTX
jQuery Fundamentals
Gil Fink
 
PDF
Jqeury ajax plugins
Inbal Geffen
 
PPTX
AngularJS Services
Eyal Vardi
 
ODP
JQuery introduction
Pradeep Saraswathi
 
PPTX
jQuery
Dileep Mishra
 
PDF
Your Entity, Your Code
Marco Vito Moscaritolo
 
PDF
droidQuery: The Android port of jQuery
PhDBrown
 
PPTX
Jquery plugin development
Faruk Hossen
 
PPT
Jquery presentation
Narendra Dabhi
 
PDF
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...
allilevine
 
PPT
JQuery introduction
NexThoughts Technologies
 
PDF
Doctrine For Beginners
Jonathan Wage
 
KEY
Sprout core and performance
Yehuda Katz
 
PPTX
AngularJS Compile Process
Eyal Vardi
 
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Darren Mothersele
 
Jquery Basics
Umeshwaran V
 
Functionality Focused Code Organization
Rebecca Murphey
 
jQuery Fundamentals
Gil Fink
 
Jqeury ajax plugins
Inbal Geffen
 
AngularJS Services
Eyal Vardi
 
JQuery introduction
Pradeep Saraswathi
 
Your Entity, Your Code
Marco Vito Moscaritolo
 
droidQuery: The Android port of jQuery
PhDBrown
 
Jquery plugin development
Faruk Hossen
 
Jquery presentation
Narendra Dabhi
 
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...
allilevine
 
JQuery introduction
NexThoughts Technologies
 
Doctrine For Beginners
Jonathan Wage
 
Sprout core and performance
Yehuda Katz
 
AngularJS Compile Process
Eyal Vardi
 

Similar to jQuery secrets (20)

PDF
jQuery secrets
Bastian Feder
 
PDF
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
PDF
The Dom Scripting Toolkit J Query
QConLondon2008
 
PDF
DOM Scripting Toolkit - jQuery
Remy Sharp
 
PPTX
jQuery
Jon Erickson
 
PDF
Remy Sharp The DOM scripting toolkit jQuery
deimos
 
PPTX
A to Z about JQuery - Become Newbie to Expert Java Developer
Manoj Bhuva
 
PPT
jQuery for beginners
Divakar Gu
 
KEY
jQuery - Tips And Tricks
Lester Lievens
 
PDF
DOSUG Intro to JQuery JavaScript Framework
Matthew McCullough
 
PPT
jQuery Tips Tricks Trivia
Cognizant
 
PPTX
Javascript And J Query
itsarsalan
 
KEY
Jquery Fundamentals
Rebecca Murphey
 
PDF
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Sean Burgess
 
PDF
Javascript libraries
Dumindu Pahalawatta
 
PPTX
jQuery basics for Beginners
Pooja Saxena
 
PPTX
Getting started with jQuery
Gill Cleeren
 
PDF
State of jQuery and Drupal
jeresig
 
PPT
Jquery 2
Manish Kumar Singh
 
PPTX
Introduction to Jquery
Ahmed Elharouny
 
jQuery secrets
Bastian Feder
 
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
The Dom Scripting Toolkit J Query
QConLondon2008
 
DOM Scripting Toolkit - jQuery
Remy Sharp
 
jQuery
Jon Erickson
 
Remy Sharp The DOM scripting toolkit jQuery
deimos
 
A to Z about JQuery - Become Newbie to Expert Java Developer
Manoj Bhuva
 
jQuery for beginners
Divakar Gu
 
jQuery - Tips And Tricks
Lester Lievens
 
DOSUG Intro to JQuery JavaScript Framework
Matthew McCullough
 
jQuery Tips Tricks Trivia
Cognizant
 
Javascript And J Query
itsarsalan
 
Jquery Fundamentals
Rebecca Murphey
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Sean Burgess
 
Javascript libraries
Dumindu Pahalawatta
 
jQuery basics for Beginners
Pooja Saxena
 
Getting started with jQuery
Gill Cleeren
 
State of jQuery and Drupal
jeresig
 
Introduction to Jquery
Ahmed Elharouny
 
Ad

More from Bastian Feder (18)

PDF
JQuery plugin development fundamentals
Bastian Feder
 
PDF
Why documentation osidays
Bastian Feder
 
PDF
Solid principles
Bastian Feder
 
PDF
Php unit the-mostunknownparts
Bastian Feder
 
PDF
PhpUnit - The most unknown Parts
Bastian Feder
 
PDF
Introducing TDD to your project
Bastian Feder
 
PDF
The Beauty and the Beast
Bastian Feder
 
PDF
Php unit the-mostunknownparts
Bastian Feder
 
PDF
Advanced Eclipse Workshop (held at IPC2010 -spring edition-)
Bastian Feder
 
PDF
The beautyandthebeast phpbat2010
Bastian Feder
 
PDF
Debugging PHP with xDebug inside of Eclipse PDT 2.1
Bastian Feder
 
PDF
Eclipse HandsOn Workshop
Bastian Feder
 
PDF
The Beauty And The Beast Php N W09
Bastian Feder
 
PDF
Eclipse Pdt2.0 26.05.2009
Bastian Feder
 
PDF
Php Development With Eclipde PDT
Bastian Feder
 
PDF
Php Documentor The Beauty And The Beast
Bastian Feder
 
PDF
Bubbles & Trees with jQuery
Bastian Feder
 
ODP
Ajax hands on - Refactoring Google Suggest
Bastian Feder
 
JQuery plugin development fundamentals
Bastian Feder
 
Why documentation osidays
Bastian Feder
 
Solid principles
Bastian Feder
 
Php unit the-mostunknownparts
Bastian Feder
 
PhpUnit - The most unknown Parts
Bastian Feder
 
Introducing TDD to your project
Bastian Feder
 
The Beauty and the Beast
Bastian Feder
 
Php unit the-mostunknownparts
Bastian Feder
 
Advanced Eclipse Workshop (held at IPC2010 -spring edition-)
Bastian Feder
 
The beautyandthebeast phpbat2010
Bastian Feder
 
Debugging PHP with xDebug inside of Eclipse PDT 2.1
Bastian Feder
 
Eclipse HandsOn Workshop
Bastian Feder
 
The Beauty And The Beast Php N W09
Bastian Feder
 
Eclipse Pdt2.0 26.05.2009
Bastian Feder
 
Php Development With Eclipde PDT
Bastian Feder
 
Php Documentor The Beauty And The Beast
Bastian Feder
 
Bubbles & Trees with jQuery
Bastian Feder
 
Ajax hands on - Refactoring Google Suggest
Bastian Feder
 
Ad

Recently uploaded (20)

PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
July Patch Tuesday
Ivanti
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 

jQuery secrets

  • 1. jQuery secrets Bastian Feder IPC 2011 [email protected] th 10 October 2011
  • 2. Me, myself & I JavaScript since 2002 Trainer & coach Opensource addict PHP manual translations FluentDOM phpDox ...
  • 4. Saving the state jQuery.data( element, key[, value] ) Store any kind of information on a DOM element Circular references avoided Low level function use $( element ).data( key[, value] ) instead.
  • 5. Removing the state jQuery.removeData( element[, key] ) Low level function use $( element ).removeData( [key] ) instead. Removes all data if no key is passed.
  • 6. State example var $logo = $('#jq-siteLogo'); $(document).data('logo', $logo); $logo.detach(); $('#content').before($(document).data('logo')); $(document).removeData('logo');
  • 7. Hidden events getData[.namespace] Emitted, when the content is read. setData[.namespace] Emitted, when the content is set or reset. changeData[.namespace] Emitted, when the content has been changed. Comes always in hand with the setData event.
  • 8. Hidden events (II) var foo = $('div'); foo.bind('changeData.data', function( event, key, value ) { var data = new Array(value); $('#activitystream').trigger('myCustomEvent', data); }); foo.data('my.data', 'Tux is a mascott.');
  • 9. Extending for the good jQuery.extend([deep], target[, object1][,objectN]) var empty = {}; var defaults = { validate : false, limit : {max: 5, min: 1}, name : "foo" }; var options = { validate : true, limit : {max:10} }; var settings = $.extend(true, empty, defaults, options);
  • 10. User defined properties Register of translations Used by .attr( ) jQuery.propFix = { for : "htmlFor", class : "className", … }; jQuery.extend( jQuery.propFix, {uijgh : "ui-jeopardy-gameboard-header"} );
  • 11. DOM attributes Applies for every entry in jQuery.attrFn (e.g. val, css, text, data, … ) var tux = $("<div/>", { css : {background: '#AAA'}, text : 'beastie' }); $("div")['css']({background: '#BBB'});
  • 12. AJAX
  • 13. Shortcuts (II) jQuery.getJSON( url[,data ][,callback(data, textStatus)] ) jQuery.getScript( url[,success(data, textStatus)] )
  • 14. Global AJAX events ajaxStart everytime a new request has been fired and no request s running ajaxStop everytime no request is running anymore ajaxComplete once for each completed request (success or failure)
  • 15. Global AJAX events (II) ajaxError once for each failed request ajaxSuccess once for each successful request ajaxSend once for each request, but before the request is send
  • 16. Global AJAX events (III) jQuery('#loadingIndicator').bind( "ajaxStart", function() { $(this).show(); } ); jQuery('#loadingIndicator').bind( "ajaxStop", function() { $(this).hide(); } );
  • 18. Event binding .bind(type[, data], handler(event)) 'type' might also be an object $('.clickable').bind({ 'click' : function(event) { //callback }, 'mousedown' : function(event) { //callback } });
  • 19. Namespaces $('.clickable').bind('click.namespace', function(e){}); $('.clickable').bind({ 'click.namespace': function(event) { //callback }, 'mousedown.namespace': function(event) { //callback } }); $('.clickable').unbind('.namespace');
  • 20. Selfdefined speeding jQuery.extend( jQuery.fx.speeds, { slow : 600, fast : 200, _default : 400 }, { slowmotion: 1000 } ); $('#clickme').click(function() { $('#book').fadeIn('slowmotion'); });
  • 22. jQuery plugins jQuery.error( message ) jQuery.prototype.error = function( message, errorcode ) { var error = []; error['msg'] = message; error['code'] = errorcode; if (typeof console != undefined) { console.error(error) return; } }
  • 23. jQuery UI jQuery.extend('ui.autosuggest.prototype, { _search: function( value ) { // always save the actual value, // not the one passed as an argument this.term = this.element .addClass( "ui-autocomplete-loading" .val(); this.source( { term: value }, this.response ); });
  • 25. Slides'n contact Please comment the talk on joind.in http://joind.in/3833 Slides http://slideshare.net/lapistano Email: [email protected]
  • 26. License This set of slides and the source code included in the download package is licensed under the Creative Commons Attribution- Noncommercial-Share Alike 2.0 Generic License