Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Developing a jQuery Plugin
Search
James Hughes
October 22, 2010
Programming
3
200
Developing a jQuery Plugin
Old presentation from the archives.
James Hughes
October 22, 2010
Tweet
Share
More Decks by James Hughes
See All by James Hughes
Functional Programming with Clojure
kouphax
1
120
Tyrannosaurus Rx
kouphax
0
120
React
kouphax
2
710
Play for (Java|Scala)
kouphax
0
110
Devops: A Case Study
kouphax
0
77
Scala for C# Developers
kouphax
5
2.6k
Dropwizard - Production Ready Web Services
kouphax
3
1.5k
Scala for Fun & Profit
kouphax
4
630
What Agile Means To Me
kouphax
0
140
Other Decks in Programming
See All in Programming
AIネイティブなプロダクトをGolangで挑む取り組み
nmatsumoto4
0
120
都市をデータで見るってこういうこと PLATEAU属性情報入門
nokonoko1203
1
550
ASP.NETアプリケーションのモダナイズ インフラ編
tomokusaba
1
390
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
770
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
130
Spring gRPC で始める gRPC 入門 / Introduction to gRPC with Spring gRPC
mackey0225
2
520
設計やレビューに悩んでいるPHPerに贈る、クリーンなオブジェクト設計の指針たち
panda_program
3
420
WindowInsetsだってテストしたい
ryunen344
1
190
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
120
XSLTで作るBrainfuck処理系
makki_d
0
210
ReadMoreTextView
fornewid
1
450
GoのWebAssembly活用パターン紹介
syumai
3
10k
Featured
See All Featured
Scaling GitHub
holman
459
140k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
BBQ
matthewcrist
89
9.7k
Being A Developer After 40
akosma
90
590k
The Pragmatic Product Professional
lauravandoore
35
6.7k
For a Future-Friendly Web
brad_frost
179
9.8k
Designing for humans not robots
tammielis
253
25k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
A designer walks into a library…
pauljervisheath
206
24k
Transcript
Introduc)on to jQuery
Developing a Plugin
The Original Func)on /* Make Element Standout */
func)on standout(element){ element.style.border = “1px solid #f00”; } standout(document.getElementById(‘myelement’) );
Steps 1. Convert to Plugin 2. Encapsulate Plugin
Specific Code 3. Support Chaining 4. Expose Global Defaults 5. Support “Per-‐Call” ConfiguraCon 6. Support “Per-‐Element” ConfiguraCon
As a Plugin /* plugin code */
$.fn.standout = func)on() this.css(“border”, “1px solid #f00”); } $(“div”).standout()
Encapsulate Plugin Specific Code (func)on($){
/* plugin code */ $.fn.standout = func)on() debug(this); this.css(“border”, “1px solid #f00”); }; /* private debug method */ func)on debug(o) { console.log(“Count:” + o.length); } })(jQuery);
Facilitate Chaining (func)on($){ /*
plugin code */ $.fn.standout = func)on() debug(this); return this.css(“border”, “1px solid #f00”); } func)on debug(o) { … } })(jQuery); $(“div”).standout().fadeOut()
Expose Global Defaults /* plugin code
*/ $.fn.standout = func)on() if($.fn.standout.defaults.debug){ debug(this); } return this.css( "border", $.fn.standout.defaults.borderStyle ); } /* global defaults*/ $.fn.standout.defaults = { borderStyle : "1px solid #f00” debug: window.console && window.console.log }
Support “Per-‐Call” Configura)on /* plugin code
*/ $.fn.standout = func)on(opts) var o = $.extend({}, $.fn.standout.defaults, opts); return this.css("border", o.borderStyle); }; /* global defaults */ $.fn.standout.defaults = { borderStyle : "1px solid #f00” debug: window.console && window.console.log } $(“div”).standout({debug:false})
Support “Per-‐Element” Configura)on /* plugin code
*/ $.fn.standout = func)on(opts) var o = $.extend({}, $.fn.standout.defaults, opts); return this.each(func)on() { var t = $(this); var eo = $.meta ? $.extend({}, o, t.data()) : o; t.css("border", eo.borderStyle); }); }; <div class=“{borderStyle: ‘1px solid blue’}”></div>
Sta)c Plugins
getParam Method (func)on($){ /* extract
a parameter from URL */ $.getParam = func)on(name) { var param = new RegExp( '[\\?&]' + name + '=([^&#]*)’ ).exec(window.loca)on.href); return (param) ? param[1] : null; }; })(jQuery); // hip://www.mysite.com/?username=jameshu $.getParam(“username”); // <-‐-‐ “jameshu”
getParam Method (Alterna)ve) $.extend({
/* extract a parameter from URL */ getParam : func)on(name) { var param = new RegExp( '[\\?&]' + name + '=([^&#]*)’ ).exec(window.loca)on.href); return (param) ? param[1] : null; } });
Pseudo Selectors
Modified Input Field Selector /* Add Modified Input Field
Selector*/ $.extend($.expr[':'], { modified: func)on(el, idx, tokens) { /* tokens[3] = args*/ return $(el).is("input") && el.defaultValue != el.value; } }); /* Clear all modified input elements */ $("input:modified") .val(“”);
Ques)ons?