developing jquery plugins with ease

82
Developing jQuery Plugins creating an edit-on-demand plugin Jakob Westhoff <[email protected]> FrOSCon 2009 August 22, 2009 http://westhoffswelt.de jakob@westhoffswelt.de slide: 1 / 36

Upload: westhoff

Post on 17-May-2015

2.846 views

Category:

Technology


0 download

DESCRIPTION

Presentation given at the FrOSCon '09 demonstrating how to create a simple jQuery plugin.

TRANSCRIPT

Page 1: Developing jQuery Plugins with Ease

Developing jQuery Pluginscreating an edit-on-demand plugin

Jakob Westhoff <[email protected]>

FrOSCon 2009August 22, 2009

http://westhoffswelt.de [email protected] slide: 1 / 36

Page 2: Developing jQuery Plugins with Ease

About Me

Jakob Westhoff

Computer science student at the TU Dortmund

Web-developer for more than 6 years

Author of Activebar2 (used by http://ie6update.com)

Active in different Open Source projects

http://westhoffswelt.de [email protected] slide: 2 / 36

Page 3: Developing jQuery Plugins with Ease

jQuery about itself

From the jQuery Website:

jQuery is a fast and concise JavaScript Library that simplifiesHTML document traversing, event handling, animating, and Ajaxinteractions for rapid web development. jQuery is designed tochange the way that you write JavaScript.

http://westhoffswelt.de [email protected] slide: 3 / 36

Page 4: Developing jQuery Plugins with Ease

jQuery about itself

From the jQuery Website:

jQuery is a fast and concise JavaScript Library that simplifiesHTML document traversing, event handling, animating, and Ajaxinteractions for rapid web development. jQuery is designed tochange the way that you write JavaScript.

http://westhoffswelt.de [email protected] slide: 3 / 36

Page 5: Developing jQuery Plugins with Ease

jQuery about itself

From the jQuery Website:

jQuery is a fast and concise JavaScript Library that simplifiesHTML document traversing, event handling, animating, and Ajaxinteractions for rapid web development. jQuery is designed tochange the way that you write JavaScript.

http://westhoffswelt.de [email protected] slide: 3 / 36

Page 6: Developing jQuery Plugins with Ease

jQuery about itself

From the jQuery Website:

jQuery is a fast and concise JavaScript Library that simplifiesHTML document traversing, event handling, animating, and Ajaxinteractions for rapid web development. jQuery is designed tochange the way that you write JavaScript.

http://westhoffswelt.de [email protected] slide: 3 / 36

Page 7: Developing jQuery Plugins with Ease

jQuery about itself

From the jQuery Website:

jQuery is a fast and concise JavaScript Library that simplifiesHTML document traversing, event handling, animating, and Ajaxinteractions for rapid web development. jQuery is designed tochange the way that you write JavaScript.

http://westhoffswelt.de [email protected] slide: 3 / 36

Page 8: Developing jQuery Plugins with Ease

Introduction to jQuery

Compact

only 56kb minified19kb minified and gzipped

Cross-browser compatible

Internet Explorer 6.0+Firefox 2+Safari 3.0+Opera 9.0+Chrome

Easily extendable

http://westhoffswelt.de [email protected] slide: 4 / 36

Page 9: Developing jQuery Plugins with Ease

Introduction to jQuery

Compact

only 56kb minified19kb minified and gzipped

Cross-browser compatible

Internet Explorer 6.0+Firefox 2+Safari 3.0+Opera 9.0+Chrome

Easily extendable

http://westhoffswelt.de [email protected] slide: 4 / 36

Page 10: Developing jQuery Plugins with Ease

Introduction to jQuery

Compact

only 56kb minified19kb minified and gzipped

Cross-browser compatible

Internet Explorer 6.0+Firefox 2+Safari 3.0+Opera 9.0+Chrome

Easily extendable

http://westhoffswelt.de [email protected] slide: 4 / 36

Page 11: Developing jQuery Plugins with Ease

jQuery Example

$ ( ”p#example ” ) . addC la s s ( ” h i g h l i g h t ” ) . f a d e I n ( ” s low ” ) ;

Find all paragraphs with the id example

Add the css class highlight to them

Fade in the paragraph slowly

http://westhoffswelt.de [email protected] slide: 5 / 36

Page 12: Developing jQuery Plugins with Ease

jQuery Example

$ ( ”p#example ” ) . addC la s s ( ” h i g h l i g h t ” ) . f a d e I n ( ” s low ” ) ;

Find all paragraphs with the id example

Add the css class highlight to them

Fade in the paragraph slowly

http://westhoffswelt.de [email protected] slide: 5 / 36

Page 13: Developing jQuery Plugins with Ease

jQuery Example

$ ( ”p#example ” ) . addC la s s ( ” h i g h l i g h t ” ) . f a d e I n ( ” s low ” ) ;

Find all paragraphs with the id example

Add the css class highlight to them

Fade in the paragraph slowly

http://westhoffswelt.de [email protected] slide: 5 / 36

Page 14: Developing jQuery Plugins with Ease

jQuery Example

$ ( ”p#example ” ) . addC la s s ( ” h i g h l i g h t ” ) . f a d e I n ( ” s low ” ) ;

Find all paragraphs with the id example

Add the css class highlight to them

Fade in the paragraph slowly

http://westhoffswelt.de [email protected] slide: 5 / 36

Page 15: Developing jQuery Plugins with Ease

Working with jQuery

$ ( ”p#example ” ) . addC la s s ( ” h i g h l i g h t ” ) . f a d e I n ( ” s low ” ) ;

Accessed using $ or jQuery

Document centric

$(css selector).operation

Fluent interface paradigm

operation().operation().operation()

http://westhoffswelt.de [email protected] slide: 6 / 36

Page 16: Developing jQuery Plugins with Ease

Working with jQuery

$ ( ”p#example ” ) . addC la s s ( ” h i g h l i g h t ” ) . f a d e I n ( ” s low ” ) ;

Accessed using $ or jQuery

Document centric

$(css selector).operation

Fluent interface paradigm

operation().operation().operation()

http://westhoffswelt.de [email protected] slide: 6 / 36

Page 17: Developing jQuery Plugins with Ease

Working with jQuery

$ ( ”p#example ” ) . addC la s s ( ” h i g h l i g h t ” ) . f a d e I n ( ” s low ” ) ;

Accessed using $ or jQuery

Document centric

$(css selector).operation

Fluent interface paradigm

operation().operation().operation()

http://westhoffswelt.de [email protected] slide: 6 / 36

Page 18: Developing jQuery Plugins with Ease

Beginning Plugin Development

Before you start

Check http://plugins.jquery.com/

Read Plugins/Authoring documentation

http://docs.jquery.com/Plugins/Authoring

http://westhoffswelt.de [email protected] slide: 7 / 36

Page 19: Developing jQuery Plugins with Ease

Beginning Plugin Development

Before you start

Check http://plugins.jquery.com/

Read Plugins/Authoring documentation

http://docs.jquery.com/Plugins/Authoring

http://westhoffswelt.de [email protected] slide: 7 / 36

Page 20: Developing jQuery Plugins with Ease

Introducing the Example

On-Demand-Editing Plugin

Used by Flickr and others

http://westhoffswelt.de [email protected] slide: 8 / 36

Page 21: Developing jQuery Plugins with Ease

Plugin Requirements

Applyable to every block level element

Capture on mouseover / mouseout events for color change onhovering

Transform content to edit box and save/cancel buttons onclick

Execute user-definable callback function once new data isprovided

http://westhoffswelt.de [email protected] slide: 9 / 36

Page 22: Developing jQuery Plugins with Ease

Plugin Requirements

Applyable to every block level element

Capture on mouseover / mouseout events for color change onhovering

Transform content to edit box and save/cancel buttons onclick

Execute user-definable callback function once new data isprovided

http://westhoffswelt.de [email protected] slide: 9 / 36

Page 23: Developing jQuery Plugins with Ease

Plugin Requirements

Applyable to every block level element

Capture on mouseover / mouseout events for color change onhovering

Transform content to edit box and save/cancel buttons onclick

Execute user-definable callback function once new data isprovided

http://westhoffswelt.de [email protected] slide: 9 / 36

Page 24: Developing jQuery Plugins with Ease

Plugin Requirements

Applyable to every block level element

Capture on mouseover / mouseout events for color change onhovering

Transform content to edit box and save/cancel buttons onclick

Execute user-definable callback function once new data isprovided

http://westhoffswelt.de [email protected] slide: 9 / 36

Page 25: Developing jQuery Plugins with Ease

Let’s begin

Choose a name for our plugin: editable

Create jquery.editable.cssHover effectTextbox look and feelButton positioning / look and feel

http://westhoffswelt.de [email protected] slide: 10 / 36

Page 26: Developing jQuery Plugins with Ease

Stylesheet for our Plugin

Choose a name for our plugin: editable

Create jquery.editable.cssHover effectTextbox look and feelButton positioning / look and feel

http://westhoffswelt.de [email protected] slide: 10 / 36

Page 27: Developing jQuery Plugins with Ease

Stylesheet for our Plugin

1 . e d i t a b l e h o v e r ,2 . e d i t a b l e a c t i v e3 {4 background−c o l o r : #e f e e e e ;5 }67 . e d i t a b l e c o n t a i n e r span8 {9 font−s i z e : 12px ;

10 c o l o r : #000000;11 margin : 0 8px 0 8px ;12 }1314 . e d i t a b l e c o n t a i n e r input ,15 . e d i t a b l e c o n t a i n e r t e x t a r e a16 {17 d i s p l a y : b l o ck ;18 }

http://westhoffswelt.de [email protected] slide: 11 / 36

Page 28: Developing jQuery Plugins with Ease

The problem with the $ shortcut

$ should not be used in plugins

jQuery allows redefining of this alias for compatability reasonsMay break your plugin

Always use jQuery

Or use nifty workaround:

1 ( f u n c t i o n ( $ ) {2 . . .3 // Your p l u g i n code goes he r e4 . . .5 }) ( jQuery ) ;

http://westhoffswelt.de [email protected] slide: 12 / 36

Page 29: Developing jQuery Plugins with Ease

The problem with the $ shortcut

$ should not be used in plugins

jQuery allows redefining of this alias for compatability reasonsMay break your plugin

Always use jQuery

Or use nifty workaround:

1 ( f u n c t i o n ( $ ) {2 . . .3 // Your p l u g i n code goes he r e4 . . .5 }) ( jQuery ) ;

http://westhoffswelt.de [email protected] slide: 12 / 36

Page 30: Developing jQuery Plugins with Ease

The problem with the $ shortcut

$ should not be used in plugins

jQuery allows redefining of this alias for compatability reasonsMay break your plugin

Always use jQuery

Or use nifty workaround:

1 ( f u n c t i o n ( $ ) {2 . . .3 // Your p l u g i n code goes he r e4 . . .5 }) ( jQuery ) ;

http://westhoffswelt.de [email protected] slide: 12 / 36

Page 31: Developing jQuery Plugins with Ease

Registering the Plugin Method

Choose a name for our plugin method: editable

Register the new method in the jQuery.fn namespace:

1 jQuery . fn . e d i t a b l e = f u n c t i o n ( save fn , op t i on ) {2 . . .3 // P lug i n method4 . . .5 }

http://westhoffswelt.de [email protected] slide: 13 / 36

Page 32: Developing jQuery Plugins with Ease

Registering the Plugin Method

Choose a name for our plugin method: editable

Register the new method in the jQuery.fn namespace:

1 jQuery . fn . e d i t a b l e = f u n c t i o n ( save fn , op t i on ) {2 . . .3 // P lug i n method4 . . .5 }

http://westhoffswelt.de [email protected] slide: 13 / 36

Page 33: Developing jQuery Plugins with Ease

Handling Optional Options

Most plugins need configuration options

Use a default if a certain option is not provided

Options are supplied as associative array / object

Utilize the jQuery.extend function:

1 va r op t i on = jQuery . ex tend ({2 ” m u l t i l i n e ” : f a l s e ,3 ” p r e f i x ” : ” e d i t a b l e ”4 } , o p t i on ) ;

http://westhoffswelt.de [email protected] slide: 14 / 36

Page 34: Developing jQuery Plugins with Ease

Handling Optional Options

Most plugins need configuration options

Use a default if a certain option is not provided

Options are supplied as associative array / object

Utilize the jQuery.extend function:

1 va r op t i on = jQuery . ex tend ({2 ” m u l t i l i n e ” : f a l s e ,3 ” p r e f i x ” : ” e d i t a b l e ”4 } , o p t i on ) ;

http://westhoffswelt.de [email protected] slide: 14 / 36

Page 35: Developing jQuery Plugins with Ease

Handling Optional Options

Most plugins need configuration options

Use a default if a certain option is not provided

Options are supplied as associative array / object

Utilize the jQuery.extend function:

1 va r op t i on = jQuery . ex tend ({2 ” m u l t i l i n e ” : f a l s e ,3 ” p r e f i x ” : ” e d i t a b l e ”4 } , o p t i on ) ;

http://westhoffswelt.de [email protected] slide: 14 / 36

Page 36: Developing jQuery Plugins with Ease

Handling Optional Options

Most plugins need configuration options

Use a default if a certain option is not provided

Options are supplied as associative array / object

Utilize the jQuery.extend function:

1 va r op t i on = jQuery . ex tend ({2 ” m u l t i l i n e ” : f a l s e ,3 ” p r e f i x ” : ” e d i t a b l e ”4 } , o p t i on ) ;

http://westhoffswelt.de [email protected] slide: 14 / 36

Page 37: Developing jQuery Plugins with Ease

this Context inside the Plugin Method

The this context inside a plugin method ...

points to the called jQuery objectmay represent a set of DOM elements

Use each to handle sets correctly:

1 re tu rn $ ( t h i s ) . each ( f u n c t i o n ( ) {2 . . .3 // ” t h i s ” maps to the c u r r e n t l y hand led e l ement4 . . .5 }) ;

http://westhoffswelt.de [email protected] slide: 15 / 36

Page 38: Developing jQuery Plugins with Ease

Using each to Handle Sets

The this context inside a plugin method ...

points to the called jQuery objectmay represent a set of DOM elements

Use each to handle sets correctly:

1 re tu rn $ ( t h i s ) . each ( f u n c t i o n ( ) {2 . . .3 // ” t h i s ” maps to the c u r r e n t l y hand led e l ement4 . . .5 }) ;

http://westhoffswelt.de [email protected] slide: 15 / 36

Page 39: Developing jQuery Plugins with Ease

What we have got so far

1 ( f u n c t i o n ( $ ) {2 jQuery . fn . e d i t a b l e = f u n c t i o n ( save fn , op t i on ) {34 // Option hand l i n g5 va r op t i on = jQuery . ex tend ({6 ” op t i on ” : ” d e f a u l t v a l u e ” ,7 . . .8 } , o p t i on ) ;9

10 // Handle s e t s c o r r e c t l y .11 // Ensure the f l u e n t i n t e r f a c e paradigm .12 re tu rn $ ( t h i s ) . each ( f u n c t i o n ( ) {13 . . .14 // ” r e a l ” p l u g i n code goes i n he r e15 . . .16 }) ;1718 }19 }) ( jQuery ) ;

http://westhoffswelt.de [email protected] slide: 16 / 36

Page 40: Developing jQuery Plugins with Ease

Modifying the Container Element

Add a css class to the container element:

$ ( t h i s ) . addC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ c o n t a i n e r ’ ) ;

Register events for onhover effect:

1 $ ( t h i s ) . b ind ( ”mouseenter mouse leave ” ,2 f u n c t i o n ( e ) {3 $ ( t h i s ) . t o g g l e C l a s s (4 op t i on [ ’ p r e f i x ’ ] + ’ hove r ’5 ) ;6 }7 ) ;

http://westhoffswelt.de [email protected] slide: 17 / 36

Page 41: Developing jQuery Plugins with Ease

Modifying the Container Element

Add a css class to the container element:

$ ( t h i s ) . addC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ c o n t a i n e r ’ ) ;

Register events for onhover effect:

1 $ ( t h i s ) . b ind ( ”mouseenter mouse leave ” ,2 f u n c t i o n ( e ) {3 $ ( t h i s ) . t o g g l e C l a s s (4 op t i on [ ’ p r e f i x ’ ] + ’ hove r ’5 ) ;6 }7 ) ;

http://westhoffswelt.de [email protected] slide: 17 / 36

Page 42: Developing jQuery Plugins with Ease

Enter Edit Mode on Click

On click enter edit mode

Registered using one as once only eventCalling plugin private function enterEditMode

1 $ ( t h i s ) . one ( ’ c l i c k ’ ,2 f u n c t i o n ( ) {3 ente rEd i tMode ( $ ( t h i s ) ) ;4 }5 ) ;

http://westhoffswelt.de [email protected] slide: 18 / 36

Page 43: Developing jQuery Plugins with Ease

Private Plugin Functions

Function in the plugin scope

Access to all variables globally declared (option)

Access to all plugin method parameters (savefn)

1 jQuery . fn . e d i t a b l e = f u n c t i o n ( save fn , op t i on ) {2 va r op t i on = . . .34 re tu rn $ ( t h i s ) . each ( f u n c t i o n ( ) {5 . . .6 }) ;78 f u n c t i o n ente rEd i tMode ( c o n t a i n e r ) {9 // Access to : c on t a i n e r , s ave fn , op t i on

10 }11 }

http://westhoffswelt.de [email protected] slide: 19 / 36

Page 44: Developing jQuery Plugins with Ease

Private Plugin Functions

Function in the plugin scope

Access to all variables globally declared (option)

Access to all plugin method parameters (savefn)

1 jQuery . fn . e d i t a b l e = f u n c t i o n ( save fn , op t i on ) {2 va r op t i on = . . .34 re tu rn $ ( t h i s ) . each ( f u n c t i o n ( ) {5 . . .6 }) ;78 f u n c t i o n ente rEd i tMode ( c o n t a i n e r ) {9 // Access to : c on t a i n e r , s ave fn , op t i on

10 }11 }

http://westhoffswelt.de [email protected] slide: 19 / 36

Page 45: Developing jQuery Plugins with Ease

Private Plugin Functions

Function in the plugin scope

Access to all variables globally declared (option)

Access to all plugin method parameters (savefn)

1 jQuery . fn . e d i t a b l e = f u n c t i o n ( save fn , op t i on ) {2 va r op t i on = . . .34 re tu rn $ ( t h i s ) . each ( f u n c t i o n ( ) {5 . . .6 }) ;78 f u n c t i o n ente rEd i tMode ( c o n t a i n e r ) {9 // Access to : c on t a i n e r , s ave fn , op t i on

10 }11 }

http://westhoffswelt.de [email protected] slide: 19 / 36

Page 46: Developing jQuery Plugins with Ease

Private Plugin Functions

Function in the plugin scope

Access to all variables globally declared (option)

Access to all plugin method parameters (savefn)

1 jQuery . fn . e d i t a b l e = f u n c t i o n ( save fn , op t i on ) {2 va r op t i on = . . .34 re tu rn $ ( t h i s ) . each ( f u n c t i o n ( ) {5 . . .6 }) ;78 f u n c t i o n ente rEd i tMode ( c o n t a i n e r ) {9 // Access to : c on t a i n e r , s ave fn , op t i on

10 }11 }

http://westhoffswelt.de [email protected] slide: 19 / 36

Page 47: Developing jQuery Plugins with Ease

Needed Private Functions

enterEditMode( container )Called whenever data input should be possible

leaveEditMode( container, updatedText )Called whenever the edit process is complete and the normalstate should be restored

http://westhoffswelt.de [email protected] slide: 20 / 36

Page 48: Developing jQuery Plugins with Ease

Needed Private Functions

enterEditMode( container )Called whenever data input should be possible

leaveEditMode( container, updatedText )Called whenever the edit process is complete and the normalstate should be restored

http://westhoffswelt.de [email protected] slide: 20 / 36

Page 49: Developing jQuery Plugins with Ease

enterEditMode - Cleanup a little bit

Store the original content

va r o r i g i n a lT e x tCon t e n t = c o n t a i n e r . html ( ) ;

Disable onhover effects

c o n t a i n e r . unb ind ( ’ mouseenter mouse leave ’ ) ;c o n t a i n e r . r emoveC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ hove r ’ ) ;

Warning: This removes all mouseenter and mouseleaveevent handlers of this node

Set correct css classes

c o n t a i n e r . addC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ a c t i v e ’ ) ;

http://westhoffswelt.de [email protected] slide: 21 / 36

Page 50: Developing jQuery Plugins with Ease

enterEditMode - Cleanup a little bit

Store the original content

va r o r i g i n a lT e x tCon t e n t = c o n t a i n e r . html ( ) ;

Disable onhover effects

c o n t a i n e r . unb ind ( ’ mouseenter mouse leave ’ ) ;c o n t a i n e r . r emoveC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ hove r ’ ) ;

Warning: This removes all mouseenter and mouseleaveevent handlers of this node

Set correct css classes

c o n t a i n e r . addC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ a c t i v e ’ ) ;

http://westhoffswelt.de [email protected] slide: 21 / 36

Page 51: Developing jQuery Plugins with Ease

enterEditMode - Cleanup a little bit

Store the original content

va r o r i g i n a lT e x tCon t e n t = c o n t a i n e r . html ( ) ;

Disable onhover effects

c o n t a i n e r . unb ind ( ’ mouseenter mouse leave ’ ) ;c o n t a i n e r . r emoveC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ hove r ’ ) ;

Warning: This removes all mouseenter and mouseleaveevent handlers of this node

Set correct css classes

c o n t a i n e r . addC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ a c t i v e ’ ) ;

http://westhoffswelt.de [email protected] slide: 21 / 36

Page 52: Developing jQuery Plugins with Ease

enterEditMode - Create input field and buttons

Create the needed input field

va r e d i t = $ ( ’< i n pu t type=”t e x t”></input> ’ ) ;

Create the needed buttons

va r save = $ ( ’<button c l a s s=”save”>Save</button> ’ ) ;

va r c a n c e l =$ ( ’<button c l a s s=”c an c e l ”>Cancel </button> ’ ) ;

http://westhoffswelt.de [email protected] slide: 22 / 36

Page 53: Developing jQuery Plugins with Ease

enterEditMode - Create input field and buttons

Create the needed input field

va r e d i t = $ ( ’< i n pu t type=”t e x t”></input> ’ ) ;

Create the needed buttons

va r save = $ ( ’<button c l a s s=”save”>Save</button> ’ ) ;

va r c a n c e l =$ ( ’<button c l a s s=”c an c e l ”>Cancel </button> ’ ) ;

http://westhoffswelt.de [email protected] slide: 22 / 36

Page 54: Developing jQuery Plugins with Ease

enterEditMode - Register button onClick

Register onClick for Cancel

1 c an c e l . b ind ( ’ c l i c k ’ , f u n c t i o n ( e ) {2 e . s t opP ropaga t i on ( ) ;3 l eaveEd i tMode ( con t a i n e r , o r i g i n a lT e x tCon t e n t ) ;4 }) ;

Register onClick for Save

1 save . b ind ( ’ c l i c k ’ , f u n c t i o n ( e ) {2 e . s t opP ropaga t i on ( ) ;3 l eaveEd i tMode (4 con t a i n e r ,5 s a v e f n ( e d i t . v a l ( ) )6 ) ;7 }) ;

http://westhoffswelt.de [email protected] slide: 23 / 36

Page 55: Developing jQuery Plugins with Ease

enterEditMode - Register button onClick

Register onClick for Cancel

1 c an c e l . b ind ( ’ c l i c k ’ , f u n c t i o n ( e ) {2 e . s t opP ropaga t i on ( ) ;3 l eaveEd i tMode ( con t a i n e r , o r i g i n a lT e x tCon t e n t ) ;4 }) ;

Register onClick for Save

1 save . b ind ( ’ c l i c k ’ , f u n c t i o n ( e ) {2 e . s t opP ropaga t i on ( ) ;3 l eaveEd i tMode (4 con t a i n e r ,5 s a v e f n ( e d i t . v a l ( ) )6 ) ;7 }) ;

http://westhoffswelt.de [email protected] slide: 23 / 36

Page 56: Developing jQuery Plugins with Ease

enterEditMode - Replace current content

Replace the current content with input field and buttons

1 c o n t a i n e r . empty ( )2 . append ( e d i t )3 . append ( save )4 . append ( c a n c e l ) ;

http://westhoffswelt.de [email protected] slide: 24 / 36

Page 57: Developing jQuery Plugins with Ease

leaveEditMode - Reactivate onHover

Remove state from css classes

c o n t a i n e r . r emoveC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ a c t i v e ’ ) ;

Bind events for onHover effect

c o n t a i n e r . b ind ( ”mouseenter mouse leave ” , f u n c t i o n ( e ) {c o n t a i n e r . t o g g l e C l a s s ( op t i on [ ’ p r e f i x ’ ] + ’ hove r ’ ) ;

}) ;

http://westhoffswelt.de [email protected] slide: 25 / 36

Page 58: Developing jQuery Plugins with Ease

leaveEditMode - Reactivate onHover

Remove state from css classes

c o n t a i n e r . r emoveC la s s ( op t i on [ ’ p r e f i x ’ ] + ’ a c t i v e ’ ) ;

Bind events for onHover effect

c o n t a i n e r . b ind ( ”mouseenter mouse leave ” , f u n c t i o n ( e ) {c o n t a i n e r . t o g g l e C l a s s ( op t i on [ ’ p r e f i x ’ ] + ’ hove r ’ ) ;

}) ;

http://westhoffswelt.de [email protected] slide: 25 / 36

Page 59: Developing jQuery Plugins with Ease

leaveEditMode - Reactivate onClick

Re-register the onClick handler

1 $ ( t h i s ) . one ( ’ c l i c k ’ ,2 f u n c t i o n ( ) {3 ente rEd i tMode ( $ ( t h i s ) ) ;4 }5 ) ;

http://westhoffswelt.de [email protected] slide: 26 / 36

Page 60: Developing jQuery Plugins with Ease

leaveEditMode - Update text content

Update the container with the given text content

c o n t a i n e r . html ( updatedText ) ;

http://westhoffswelt.de [email protected] slide: 27 / 36

Page 61: Developing jQuery Plugins with Ease

Calling the plugin

Simply call the editable method on the selected node.

$ ( ”h1” ) . e d i t a b l e ( f u n c t i o n ( con t en t ) {// Do some AJAX magic he r e . . .re tu rn con t en t ;

}) ;

http://westhoffswelt.de [email protected] slide: 28 / 36

Page 62: Developing jQuery Plugins with Ease

editable Live Demo

Let’s take a look

Live Demo!

http://westhoffswelt.de [email protected] slide: 29 / 36

Page 63: Developing jQuery Plugins with Ease

6 Golden Rules of Plugin Development

1 Name your file jquery.[insert name of plugin].jseg. jquery.myplugin.js

2 Attach new methods to the jQuery.fn object

3 Attach new functions to the jQuery object itself

4 Methods have always to return the object they are working on

5 Use each to ensure your method is applied to all elements ina set

6 Always use jQuery instead of the $ shortcut in your plugins

or use the nifty trick shown

http://westhoffswelt.de [email protected] slide: 30 / 36

Page 64: Developing jQuery Plugins with Ease

6 Golden Rules of Plugin Development

1 Name your file jquery.[insert name of plugin].jseg. jquery.myplugin.js

2 Attach new methods to the jQuery.fn object

3 Attach new functions to the jQuery object itself

4 Methods have always to return the object they are working on

5 Use each to ensure your method is applied to all elements ina set

6 Always use jQuery instead of the $ shortcut in your plugins

or use the nifty trick shown

http://westhoffswelt.de [email protected] slide: 30 / 36

Page 65: Developing jQuery Plugins with Ease

6 Golden Rules of Plugin Development

1 Name your file jquery.[insert name of plugin].jseg. jquery.myplugin.js

2 Attach new methods to the jQuery.fn object

3 Attach new functions to the jQuery object itself

4 Methods have always to return the object they are working on

5 Use each to ensure your method is applied to all elements ina set

6 Always use jQuery instead of the $ shortcut in your plugins

or use the nifty trick shown

http://westhoffswelt.de [email protected] slide: 30 / 36

Page 66: Developing jQuery Plugins with Ease

6 Golden Rules of Plugin Development

1 Name your file jquery.[insert name of plugin].jseg. jquery.myplugin.js

2 Attach new methods to the jQuery.fn object

3 Attach new functions to the jQuery object itself

4 Methods have always to return the object they are working on

5 Use each to ensure your method is applied to all elements ina set

6 Always use jQuery instead of the $ shortcut in your plugins

or use the nifty trick shown

http://westhoffswelt.de [email protected] slide: 30 / 36

Page 67: Developing jQuery Plugins with Ease

6 Golden Rules of Plugin Development

1 Name your file jquery.[insert name of plugin].jseg. jquery.myplugin.js

2 Attach new methods to the jQuery.fn object

3 Attach new functions to the jQuery object itself

4 Methods have always to return the object they are working on

5 Use each to ensure your method is applied to all elements ina set

6 Always use jQuery instead of the $ shortcut in your plugins

or use the nifty trick shown

http://westhoffswelt.de [email protected] slide: 30 / 36

Page 68: Developing jQuery Plugins with Ease

6 Golden Rules of Plugin Development

1 Name your file jquery.[insert name of plugin].jseg. jquery.myplugin.js

2 Attach new methods to the jQuery.fn object

3 Attach new functions to the jQuery object itself

4 Methods have always to return the object they are working on

5 Use each to ensure your method is applied to all elements ina set

6 Always use jQuery instead of the $ shortcut in your plugins

or use the nifty trick shown

http://westhoffswelt.de [email protected] slide: 30 / 36

Page 69: Developing jQuery Plugins with Ease

Thanks for listening

Questions, comments or annotations?

Slides: http://westhoffswelt.de/portfolio.htm

Contact: Jakob Westhoff <[email protected]>

http://westhoffswelt.de [email protected] slide: 31 / 36

Page 70: Developing jQuery Plugins with Ease

Unit testing with QUnit

Unit testing? YES you want to!

QUnit

jQuerys unit testing frameworkhttp://docs.jquery.com/QUnitNo stable release, yetHowever core features are extremly stable

http://westhoffswelt.de [email protected] slide: 32 / 36

Page 71: Developing jQuery Plugins with Ease

Unit testing with QUnit

Unit testing? YES you want to!

QUnit

jQuerys unit testing frameworkhttp://docs.jquery.com/QUnitNo stable release, yetHowever core features are extremly stable

http://westhoffswelt.de [email protected] slide: 32 / 36

Page 72: Developing jQuery Plugins with Ease

Unit testing with QUnit

Unit testing? YES you want to!

QUnit

jQuerys unit testing frameworkhttp://docs.jquery.com/QUnitNo stable release, yetHowever core features are extremly stable

http://westhoffswelt.de [email protected] slide: 32 / 36

Page 73: Developing jQuery Plugins with Ease

Unit testing with QUnit

Unit testing? YES you want to!

QUnit

jQuerys unit testing frameworkhttp://docs.jquery.com/QUnitNo stable release, yetHowever core features are extremly stable

http://westhoffswelt.de [email protected] slide: 32 / 36

Page 74: Developing jQuery Plugins with Ease

Unit testing with QUnit

Unit testing? YES you want to!

QUnit

jQuerys unit testing frameworkhttp://docs.jquery.com/QUnitNo stable release, yetHowever core features are extremly stable

http://westhoffswelt.de [email protected] slide: 32 / 36

Page 75: Developing jQuery Plugins with Ease

Simple QUnit example

Include js and css

<s c r i p t s r c=” jque r y− l a t e s t . j s ”></ s c r i p t><s c r i p t type=” t e x t / j a v a s c r i p t ” s r c=” qun i t / t e s t r u n n e r .

j s ”></ s c r i p t>< l i n k r e l=” s t y l e s h e e t ” hre f=” qun i t / t e s t s u i t e . c s s ”

type=” t e x t / c s s ” media=” s c r e e n ” />

Create a simple html page to be filled with information

<h1>QUnit example</h1>

<h2 id=”banner ”></h2><h2 id=” use rAgent ”></h2>

<o l i d=” t e s t s ”></ o l>

<d iv i d=”main”></ d iv>

http://westhoffswelt.de [email protected] slide: 33 / 36

Page 76: Developing jQuery Plugins with Ease

Simple QUnit example

Include js and css

<s c r i p t s r c=” jque r y− l a t e s t . j s ”></ s c r i p t><s c r i p t type=” t e x t / j a v a s c r i p t ” s r c=” qun i t / t e s t r u n n e r .

j s ”></ s c r i p t>< l i n k r e l=” s t y l e s h e e t ” hre f=” qun i t / t e s t s u i t e . c s s ”

type=” t e x t / c s s ” media=” s c r e e n ” />

Create a simple html page to be filled with information

<h1>QUnit example</h1>

<h2 id=”banner ”></h2><h2 id=” use rAgent ”></h2>

<o l i d=” t e s t s ”></ o l>

<d iv i d=”main”></ d iv>

http://westhoffswelt.de [email protected] slide: 33 / 36

Page 77: Developing jQuery Plugins with Ease

Simple QUnit example

Define a module (optional)

module ( ”My s imp l e example module” ) ;

Add a test

t e s t ( ”My f i r s t t e s t ” , f u n c t i o n ( ) {ok ( true , ” Ev e r y t h i n g i s f i n e . ” ) ;

}) ;

http://westhoffswelt.de [email protected] slide: 34 / 36

Page 78: Developing jQuery Plugins with Ease

Simple QUnit example

Define a module (optional)

module ( ”My s imp l e example module” ) ;

Add a test

t e s t ( ”My f i r s t t e s t ” , f u n c t i o n ( ) {ok ( true , ” Ev e r y t h i n g i s f i n e . ” ) ;

}) ;

http://westhoffswelt.de [email protected] slide: 34 / 36

Page 79: Developing jQuery Plugins with Ease

Possible QUnit assertions

ok(state, [message])A boolean assertion, equivalent to JUnit’s assertTrue

equals(actual, expected, [message])A comparison assertion, equivalent to JUnit’s assertEquals

same(actual, expected, [message])A deep recursive comparison

http://westhoffswelt.de [email protected] slide: 35 / 36

Page 80: Developing jQuery Plugins with Ease

Possible QUnit assertions

ok(state, [message])A boolean assertion, equivalent to JUnit’s assertTrue

equals(actual, expected, [message])A comparison assertion, equivalent to JUnit’s assertEquals

same(actual, expected, [message])A deep recursive comparison

http://westhoffswelt.de [email protected] slide: 35 / 36

Page 81: Developing jQuery Plugins with Ease

Possible QUnit assertions

ok(state, [message])A boolean assertion, equivalent to JUnit’s assertTrue

equals(actual, expected, [message])A comparison assertion, equivalent to JUnit’s assertEquals

same(actual, expected, [message])A deep recursive comparison

http://westhoffswelt.de [email protected] slide: 35 / 36

Page 82: Developing jQuery Plugins with Ease

Assyncronous assertions

Use start() and stop() for asyncronous testing

t e s t ( ”My async t e s t ” , f u n c t i o n ( ) {s top ( ) ;se tT imeout ( f u n t i o n ( ) {

ok ( true , ” Ev e r y t h i n g i s f i n e ” ) ;s t a r t ( ) ;

} , 1000) ;}) ;

http://westhoffswelt.de [email protected] slide: 36 / 36