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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
James Hughes
October 22, 2010
Programming
210
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Developing a jQuery Plugin
Old presentation from the archives.
James Hughes
October 22, 2010
More Decks by James Hughes
See All by James Hughes
Functional Programming with Clojure
kouphax
1
160
Tyrannosaurus Rx
kouphax
0
140
React
kouphax
2
750
Play for (Java|Scala)
kouphax
0
160
Devops: A Case Study
kouphax
0
110
Scala for C# Developers
kouphax
5
2.7k
Dropwizard - Production Ready Web Services
kouphax
3
1.7k
Scala for Fun & Profit
kouphax
4
670
What Agile Means To Me
kouphax
0
180
Other Decks in Programming
See All in Programming
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
110
Android CLI
fornewid
0
170
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.2k
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
120
【やさしく解説 設計編 #1】「ドメイン駆動」と「実装駆動」ってなに? 〜設計の考え方を、たとえ話で学ぼう〜
panda728
PRO
1
130
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
150
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
500
えっ!!コードを読まずに開発を!?
hananouchi
0
230
霧の中の代数的エフェクト
funnyycat
1
430
継続モナドとリアクティブプログラミング
yukikurage
3
640
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
170
Featured
See All Featured
ラッコキーワード サービス紹介資料
rakko
1
4M
Optimizing for Happiness
mojombo
378
71k
How to make the Groovebox
asonas
2
2.3k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
320
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
640
Evolving SEO for Evolving Search Engines
ryanjones
0
240
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
410
New Earth Scene 8
popppiees
3
2.4k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Done Done
chrislema
186
16k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.8k
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?