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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
James Hughes
October 22, 2010
Programming
210
3
Share
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
150
Tyrannosaurus Rx
kouphax
0
140
React
kouphax
2
750
Play for (Java|Scala)
kouphax
0
150
Devops: A Case Study
kouphax
0
100
Scala for C# Developers
kouphax
5
2.7k
Dropwizard - Production Ready Web Services
kouphax
3
1.7k
Scala for Fun & Profit
kouphax
4
660
What Agile Means To Me
kouphax
0
170
Other Decks in Programming
See All in Programming
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
1k
UaaL×Androidアプリのメモリ計測 — Memory Profilerの先へ
rio432
0
180
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
380
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
0
110
Old Dog, New Tricks: The Java 25 Reinvention - JNation
bazlur_rahman
0
130
AI 時代のソフトウェア設計の学び方
masuda220
PRO
27
9.6k
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
300
CSC307 Lecture 17
javiergs
PRO
0
240
横断組織出身のQAEがインプロセスQAEでつまずいたこと・活かせたこと
ty89
0
320
分析エージェント精度向上における データアナリストの役割
oura_shoya
0
120
AIチームを指揮するOSS「TAKT」活用術 / How to Use “TAKT,” an OSS Tool for Orchestrating AI Teams
nrslib
5
690
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
1
1k
Featured
See All Featured
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
180
What's in a price? How to price your products and services
michaelherold
247
13k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Ethics towards AI in product and experience design
skipperchong
2
290
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
520
Between Models and Reality
mayunak
4
300
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
540
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Everyday Curiosity
cassininazir
0
210
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
560
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
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?