11
Day #7 Extending jQuery with custom plugins Wildan Maulana [email protected] http://workshop.openthinklabs.com #7

jQuery BootCamp : Extending jQuery with Custom Plugins

Embed Size (px)

Citation preview

Page 2: jQuery BootCamp : Extending jQuery with Custom Plugins

Overview

● Why to extend jQuery with custom code● The rules for effectively extending jQuery● Writing custom utility functions● Writing custom wrapper methods

Page 3: jQuery BootCamp : Extending jQuery with Custom Plugins

Why extend?

Page 4: jQuery BootCamp : Extending jQuery with Custom Plugins

The jQuery plugin authoring guidelines

● Extending jQuery takes one of two forms:● Utility functions defined directly on $ (an alias for

jQuery)● Methods to operate on a jQuery wrapped set (so-

called jQuery commands)

● Naming files and functions● Beware the $● Taming complex parameter lists●

Page 5: jQuery BootCamp : Extending jQuery with Custom Plugins

The jQuery plugin authoring guidelinesNaming files and functions

● Prefix the filename with jquery.● Follow that with the name of the plugin.● Conclude with .js.

jquery.openthinklabs.utils.js

Page 6: jQuery BootCamp : Extending jQuery with Custom Plugins

The jQuery plugin authoring guidelinesBeware the $

(function($){//// Plugin definition goes here//})(jQuery);

Page 7: jQuery BootCamp : Extending jQuery with Custom Plugins

The jQuery plugin authoring guidelinesTaming complex parameter lists

function complex(p1,p2,p3,p4,p5,p6,p7) {

complex(valueA,null,null,null,null,null,valueB);

complex(valueA,null,valueC,valueD,null,null,valueB);

complex(valueA, {p7: valueB});complex(valueA, {p3: valueC,p4: valueD,p7: valueB});

Page 8: jQuery BootCamp : Extending jQuery with Custom Plugins

The jQuery plugin authoring guidelinesTaming complex parameter lists

function complex(p1,p2,p3,p4,p5,p6,p7) {

complex(p1,options)function complex(p1,options) { var settings = $.extend({ option1: defaultValue1, option2: defaultValue2, option3: defaultValue3, option4: defaultValue4, option5: defaultValue5, option6: defaultValue6},options||{});// remainder of function...}

Page 9: jQuery BootCamp : Extending jQuery with Custom Plugins

Writing custom utility functions

$.say = function(what) { alert('I say '+what); }

jQuery.say = function(what) { alert('I say '+what); }

(function($){ $.say = function(what) { alert('I say '+what); }})(jQuery);

Page 10: jQuery BootCamp : Extending jQuery with Custom Plugins

Q&A