SlideShare una empresa de Scribd logo
1 de 62
Extensible Plugin Development
      Shane Riley (@shaneriley)

             Hashrocket
Plugins!
Plugins!


Not just for slideshows and autocompletes
Plugins!


Not just for slideshows and autocompletes
Abstract repetitive code for reuse
Plugins!


Not just for slideshows and autocompletes
Abstract repetitive code for reuse
Allow customization with options
Plugins!


Not just for slideshows and autocompletes
Abstract repetitive code for reuse
Allow customization with options
Portability between projects
Plugins!


Not just for slideshows and autocompletes
Abstract repetitive code for reuse
Allow customization with options
Portability between projects
Easier to test
Anatomy of a Plugin
http://docs.jquery.com/Plugins/Authoring
Got an Itch to Scratch?
There’s a Plugin for That!
Put the Plugin to Use
Put the Plugin to Use
Put the Plugin to Use
Put the Plugin to Use
Put the Plugin to Use
Oh Noes!




http://www.cutestpaw.com/images/oh-noes/
Rebind Your Plugin
Modify Plugin
What About Updating?
Changing Options After the Fact
Changing Options After the Fact


 Difficult to change options once initialized
Changing Options After the Fact


 Difficult to change options once initialized
 May need to unbind/rebind events
Changing Options After the Fact


 Difficult to change options once initialized
 May need to unbind/rebind events
 May need to keep track of all elements with plugin
 called on them for this purpose
Changing Options After the Fact


 Difficult to change options once initialized
 May need to unbind/rebind events
 May need to keep track of all elements with plugin
 called on them for this purpose
 Difficult or impossible to unbind only those events
Proposed Improvements
Proposed Improvements


Provide a plugin name as object property
Proposed Improvements


Provide a plugin name as object property
Namespace event bindings
Proposed Improvements


Provide a plugin name as object property
Namespace event bindings
Optionally live binding event or binding to a
context
Where to Start?
http://docs.jquery.com/Plugins/Authoring#Events
Store Methods in Plugin Object
Add Ability to Call Method
Initialize Plugin
Let’s Start Rewriting
Create Plugin Object
Move Event to Object Method
Create Initializer Method
Why Call the Method?
Initialize the Plugin
Create Unique Plugin Instance
Store Instance and Initialize
Override the Toggle Method
Override Original Method
Wait, what about our new option?
Add an anim_method Default
Change Method Name
Other Optimizations
Create a Tear-down Method
Add Live Binding and Contexts
Options:




Binding:
Bind Event to Parent Element
Bringing it all together
New Development Pattern
New Development Pattern

Created internal plugin object to contain
defaults and methods
New Development Pattern

Created internal plugin object to contain
defaults and methods
Ability to call methods directly
New Development Pattern

Created internal plugin object to contain
defaults and methods
Ability to call methods directly
Create and store standalone plugin instances
New Development Pattern

Created internal plugin object to contain
defaults and methods
Ability to call methods directly
Create and store standalone plugin instances
Plugin namespaced
New Development Pattern

Created internal plugin object to contain
defaults and methods
Ability to call methods directly
Create and store standalone plugin instances
Plugin namespaced
Teardown method
Benefits to This Method
Benefits to This Method


Can write your own overrides to suit your
individual needs
Benefits to This Method


Can write your own overrides to suit your
individual needs
Upgrading to new plugin version is worry-free
Benefits to This Method


Can write your own overrides to suit your
individual needs
Upgrading to new plugin version is worry-free
Overriding options, callbacks, methods much
easier
Recommended Reading



jQuery Plugins are Broken: http://vurl.me/TPUT
Building Stateful jQuery Plugins: http://vurl.me/TPUU
jQuery Plugin Patterns: http://vurl.me/TPUY
Twitter: @shaneriley
GitHub: shaneriley
Web: shaneriley.com
This Presentation: http://vurl.me/TTUZ

Más contenido relacionado

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destacado (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Extensible jQuery Plugin Development

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. Typical plugin:\nAttach initializing function as method to $.fn\nTake options object as argument\nExtend defaults with passed in options\nRun plugin code on each element in collection\n
  8. Our client would like an accordion effect on their blog for the rest of the post content.\n
  9. The plugin development community is huge, and there is always more than one plugin for what you’re looking for.\n
  10. Contrived example of plugin. Note that many options you’d normally see in jQuery UI accordion are omitted for brevity.\n
  11. Extend defaults with options\n
  12. Return collection for chainability\n
  13. Cache jQuery object for click trigger and work from there\n
  14. Initialize plugin event by calling with no options\n
  15. Requirements change, and the animation method needs to change once a user has clicked on an anchor. This falls outside of what the plugin is capable of, and will require modifying the plugin to achieve.\n
  16. To do this, you would unbind the click event and reinitialize the plugin with the new option, but you’ll need to add the option to the plugin as well.\n
  17. In the plugin, add anim_method and use that property to call the appropriate method when animating.\n
  18. If plugin needs to be updated, changes need to be retrofitted. A pull request could be submitted (if using Git), but not guaranteed it will be introduced into master.\n
  19. \n
  20. \n
  21. \n
  22. \n
  23. Read plugin name from plugin object. Used for both namespacing and to provide a single point of editing when naming conflicts arise.\n
  24. Read plugin name from plugin object. Used for both namespacing and to provide a single point of editing when naming conflicts arise.\n
  25. Read plugin name from plugin object. Used for both namespacing and to provide a single point of editing when naming conflicts arise.\n
  26. \n
  27. This method of plugin development employs a local object to store the plugin methods. Reposition - update will be attached to events in this case.\n
  28. If a string matching a method name is passed in, the method is called with the jQuery collection as the context. This allows you to refer to this in your methods as the jQuery collection. All other arguments to the tooltip method are passed through.\n
  29. Init method binds the events related to the plugin.\n
  30. \n
  31. Abstract the defaults to object properties on the parent accordion object. Added name property to represent the plugin name.\n
  32. Toggle callback for the click event is placed in method.\n
  33. Init method used to bind toggle method to click handler. Note event is namespaced to match plugin name. Toggle method is called within an anonymous function in order to provide the ability to rewrite the toggle method after the event is bound.\n
  34. If method is changed after initialization, we’re calling the new method instead of the pointer to the old one. This eliminates having to unbind and rebind events when the callback method changes.\n
  35. Plugin name is derived from name property on our accordion object. Much easier to change plugin name this way if conflicts arise.\nFirst condition provides ability to call methods by name on an element collection.\n\n
  36. Second condition will run plugin initialization. Separate copy of plugin created for encapsulation, each element added as a property on its parent object.\n
  37. Data object written to store standalone instance for access outside plugin.\nInit called.\n
  38. This is what our method override will now look like.\n
  39. To override the toggle method at this point, accordion object is read and the toggle method is overridden.\n
  40. What about our animation method option? Let’s add that back in.\n
  41. Create a default property on the accordion object in the plugin.\n
  42. The override method is now no longer needed, as we can override the animation method being called from outside the plugin.\n
  43. \n
  44. In our plugin example, this will unbind all namespaced methods and remove any data stored under the plugin name on the elements.\n
  45. We can also add live binding and a context option to reduce the number of event bindings.\nIf context is set, event is bound to the context rather than each element individually.\nOtherwise event is live bound.\n
  46. To bind to a parent element with a class of details, in our initializer we pass the context property.\n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. Why you should consider writing your own plugins\nHow to use jQuery UI’s widget factory to aid in plugin development\nAddy Osmani and Zeno Rocha’s repo with recommended jQuery plugin patterns\n
  57. \n