jquery plugin

8
jQuery Plugins jQuery Plugins Authoring Code for Reuse Authoring Code for Reuse

Upload: rbiggs

Post on 15-Jan-2015

727 views

Category:

Technology


5 download

DESCRIPTION

This was my presentation at Silicon Valley Code Camp 09. The material can be downloaded from : http://my/personal/rbiggs/Blog/default.aspx

TRANSCRIPT

Page 1: jQuery Plugin

jQuery PluginsjQuery PluginsjQuery PluginsjQuery Plugins

Authoring Code for ReuseAuthoring Code for ReuseAuthoring Code for ReuseAuthoring Code for Reuse

Page 2: jQuery Plugin

ModularizationModularizationModularizationModularization

Write once, throw it away.Write once, throw it away.

Write once use it again.Write once use it again.

Initial cost in time to implement, savings Initial cost in time to implement, savings

realized later.realized later.

Page 3: jQuery Plugin

jQuery IntegrationjQuery IntegrationjQuery IntegrationjQuery Integration

Attach it directly to the jQuery object.Attach it directly to the jQuery object.

• $.plugIn1 = function() {$.plugIn1 = function() {

• console.log("This is plugin ONE!");console.log("This is plugin ONE!");

• };};

Page 4: jQuery Plugin

Put your code inside a jQuery closure:Put your code inside a jQuery closure:

• (function($) {(function($) {• // Define method here.// Define method here.• $.plugIn2 = function() {$.plugIn2 = function() {• console.log("Plugin TWO is console.log("Plugin TWO is working!");working!");• };};• })(jQuery);})(jQuery);

Page 5: jQuery Plugin

Use jQuery’s extend method:Use jQuery’s extend method:• $.fn.extend({$.fn.extend({• plugIn4 : function() {plugIn4 : function() {

• this.plugIn4.number = 1;this.plugIn4.number = 1;• return this.each(function(){return this.each(function(){

• console.log("This is element console.log("This is element number: " number: " • + $.fn.plugIn4.number );+ $.fn.plugIn4.number );

• $.fn.plugIn4.number += 1;$.fn.plugIn4.number += 1;• });});• }}• });});

Page 6: jQuery Plugin

We can enclose the extended plugin in a jQuery We can enclose the extended plugin in a jQuery

closure and give it options:closure and give it options:

• (function($) {(function($) {•$.fn.extend({$.fn.extend({• hilite : function(options) {hilite : function(options) {

•return this.each(function() {return this.each(function() {• // Define what you want to do with your items.// Define what you want to do with your items.

• });});• }}

• });});• })(jQuery);})(jQuery);

Page 7: jQuery Plugin

Attaching code directly to the jQuery object is the Attaching code directly to the jQuery object is the

most limited type of plugin. This is a very poor most limited type of plugin. This is a very poor

choice.choice.

Using a jQuery closure provides namespacing, Using a jQuery closure provides namespacing,

but lacks built-in jQuery functionality. This is but lacks built-in jQuery functionality. This is

appropriate for many types of widgetsappropriate for many types of widgets

Using the jQuery extend method gives your Using the jQuery extend method gives your

plugin all the built-in power that native jQuery plugin all the built-in power that native jQuery

methods have.methods have.

Page 8: jQuery Plugin

We’re Hiring!We’re Hiring!We’re Hiring!We’re Hiring!

Bee Tan, Hiring Bee Tan, Hiring ManagerManager

510 307-8225510 307-8225