Making a simple jQuery plug-in

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

1 Favorite

Making a simple jQuery plug-in - Presentation Transcript

  1. Making a simple jQuery plug-in Dylan Fogarty-MacDonald http://dylanfm.com @dylanfm
  2. jQuery jquery.com A Javascript library.
  3. jQuery plug-ins Two types of plug-ins: Utility functions jQuery.map( array, callback ) jQuery commands $( " p " ).click( function )
  4. An example of writing a little plug-in Spock * A jQuery command which will (hopefully) find proper nouns in the elements' text and wrap them in a span with a class of keyword. $( " p").spock(); * I was watching an episode of Star Trek at the time.
  5. Steps
    • Create a file for the plug-in
  6. 1. Create a file for the plug-in Naming convention: jquery.plug-in-name.js In Spock's case: jquery.spock.js
  7. Steps
    • Create a file for the plug-in
    • Make a closure
  8. 2. Make a closure All plug-in code should be within a closure. Pass jQuery to the closure and alias it to $ . This allows you to safely use $ within your plug-in code (and not worry about clashing with other libraries or frameworks who use $ too). (function ($) { // 1227 plug-1n c0dez go here })(jQuery);
  9. Steps
    • Create a file for the plug-in
    • Make a closure
    • Define the jQuery command
  10. 3. Define the jQuery command Extend the $.fn object. * (function ($) { $.fn.extend({ spock: function() { // Stop. Hammertime! } }); })(jQuery); * $.fn is an alias for the prototype property of the jQuery constructor function.
  11. Steps
    • Create a file for the plug-in
    • Make a closure
    • Define the jQuery command
    • Return the jQuery wrapped set from function body
  12. 4. Return jQuery wrapped set jQuery hackers don't want their chains broken. $("#jQuery") .find("haxors") .give("wine") .and("cheese"); Return a jQuery wrapped set so your plug-in can take part in the chain: return $(this);
  13. Steps
    • Create a file for the plug-in
    • Make a closure
    • Define the jQuery command
    • Return the jQuery wrapped set from function body
    • Set up options hash
  14. 5. Set up options hash Users of the plug-in will want to configure it. Set up a hash of defaults and allow them to be overridden by options passed to the plug-in function when it's called. $("p").spock({ spanClass: "encino-man" });
  15. 5. Set up options hash (function ($) { var opts; $.fn.extend({ spock: function( options ) { opts = $.extend({},$.fn.spock.defaults,options); } }); $.fn.spock.defaults = { spanClass: "keyword" }; })(jQuery);
  16. Steps
    • Create a file for the plug-in
    • Make a closure
    • Define the jQuery command
    • Return the jQuery wrapped set from function body
    • Set up options hash
    • Private methods
  17. 6. Private methods $.fn.extend({ spock: function(options) { opts = $.extend({},$.fn.spock.defaults,options); return this.each(processElements); } }); function processElements () { // Frodo, don't put on the ring }
  18. Steps
    • Create a file for the plug-in
    • Make a closure
    • Define the jQuery command
    • Return the jQuery wrapped set from function body
    • Set up options hash
    • Private methods
    • Events
  19. 7. Events Your users will probably want to customise functionality. Assign a private method to a default option. Call the method through its option property. This way the users can hook into events via the options hash. $(&quot;p&quot;).spock({ processKeyword: function (match) { return &quot;<strong>&quot;+match+&quot;</strong>&quot;; } });
  20. 7. Events $.fn.spock.defaults = { processKeyword: processKeyword }; function processElements () { // ... return opts.processKeyword(match); // ... } function processKeyword (match) { // Kiss my aura, Dora. }
  21. Thanks The Spock code is on Github: http://github.com/DylanFM/jquery-spock I'll put these slides online and tweet it through: @dylanfm

+ Dylan Fogarty-MacDonaldDylan Fogarty-MacDonald, 5 months ago

custom

879 views, 1 favs, 0 embeds more stats

A lightning demo I did at RoRO (Ruby on Rails Ocean more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 879
    • 879 on SlideShare
    • 0 from embeds
  • Comments 1
  • Favorites 1
  • Downloads 13
Most viewed embeds

more

All embeds

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories