Writing Reusable Web Components with jQuery and jQuery UI

70
Reusable Web Components with jQuery and jQuery UI Slides By: Ynon Perek. http://ynonperek.com Friday, February 8, 13

description

Getting started with writing jQuery plugins and jQuery UI Widgets. Concepts, examples and recommended plugins.

Transcript of Writing Reusable Web Components with jQuery and jQuery UI

Page 1: Writing Reusable Web Components with jQuery and jQuery UI

Reusable Web Componentswith jQuery and jQuery UI

Slides By: Ynon Perek.

http://ynonperek.com

Friday, February 8, 13

Page 2: Writing Reusable Web Components with jQuery and jQuery UI

Agenda• Reusable Components

• jQuery Plugins Into

• Writing Our First Plugin

• Popular jQuery Plugins

• jQuery UI Widgets

• Writing Our First Widget

Friday, February 8, 13

Page 4: Writing Reusable Web Components with jQuery and jQuery UI

Reusable Component

• Markup + Style + JavaScript

• All are optional

Friday, February 8, 13

Page 5: Writing Reusable Web Components with jQuery and jQuery UI

Why Reuse

• Use same code within the same project

• Use same code within different projects

Friday, February 8, 13

Page 6: Writing Reusable Web Components with jQuery and jQuery UI

The Hows

• Roll Your Own

• Extend Existing Framework

Friday, February 8, 13

Page 7: Writing Reusable Web Components with jQuery and jQuery UI

The Hows

• Roll Your Own

• Extend Existing Framework

Friday, February 8, 13

Page 8: Writing Reusable Web Components with jQuery and jQuery UI

jQuery Plugins

Friday, February 8, 13

Page 9: Writing Reusable Web Components with jQuery and jQuery UI

jQuery Plugin

• Reusable JS Code

• Uses jQuery

Friday, February 8, 13

Page 11: Writing Reusable Web Components with jQuery and jQuery UI

Common Concepts

 $('#example').dataTable();

$(".dial").knob();

$('.info').powerTip({    placement: 'ne' });

Friday, February 8, 13

Page 12: Writing Reusable Web Components with jQuery and jQuery UI

Common Concepts

• Plugins add functionality to jQuery Objects

• Can take options

• Launch and Forget

 $('#example').dataTable();

$(".dial").knob();

$('.info').powerTip({    placement: 'ne' });

Friday, February 8, 13

Page 13: Writing Reusable Web Components with jQuery and jQuery UI

• jQuery plugins are just organized reusable code

Friday, February 8, 13

Page 14: Writing Reusable Web Components with jQuery and jQuery UI

Let’s Write A Plugin

Friday, February 8, 13

Page 15: Writing Reusable Web Components with jQuery and jQuery UI

Outliner Plugin

• Our plugin will add outline to an element

Friday, February 8, 13

Page 16: Writing Reusable Web Components with jQuery and jQuery UI

Writing A Plugin

• Protect the $ with a self invoking function

// Outside $ may be another library (function($) {    // But inside it's jQuery }(jQuery));

Friday, February 8, 13

Page 17: Writing Reusable Web Components with jQuery and jQuery UI

Writing A Plugin

• Extend jQuery with your plugin name

• Plugin is just a function

(function($) {   $.fn.outliner = function() {   };  }(jQuery));

Friday, February 8, 13

Page 18: Writing Reusable Web Components with jQuery and jQuery UI

Writing A Plugin

• Do you thing

• this is the jQuery object you’re working on

(function($) {   $.fn.outliner = function() {     this.css('outline', '2px solid blue');  };  }(jQuery));

Friday, February 8, 13

Page 19: Writing Reusable Web Components with jQuery and jQuery UI

Writing A Plugin

• end with return this to allow chaining

(function($) {   $.fn.outliner = function() {    this.css('outline', '2px solid blue');    return this;  };  }(jQuery));

Friday, February 8, 13

Page 20: Writing Reusable Web Components with jQuery and jQuery UI

Using The Plugin

• Here’s how a user may add outline

$('div').outliner();

Friday, February 8, 13

Page 21: Writing Reusable Web Components with jQuery and jQuery UI

Using The Plugin

• Here’s how a user may add outline

• Or with chaining

$('div').outliner().html('Cool Plugin');

Friday, February 8, 13

Page 22: Writing Reusable Web Components with jQuery and jQuery UI

Adding Options

• Some users like different color

• Some users like different width

Friday, February 8, 13

Page 23: Writing Reusable Web Components with jQuery and jQuery UI

Adding Options

• Some users like different color

• Some users like different width

$('div').outliner({  color: 'red',  width: 2});

Friday, February 8, 13

Page 24: Writing Reusable Web Components with jQuery and jQuery UI

Adding Options(function($) {   $.fn.outliner = function( options ) {    options = $.extend({}, $.fn.outliner.defaults, options);     return this.css({      'outline-color' : options.color,      'outline-width' : options.width + 'px',      'outline-style' : 'solid'    });  };   $.fn.outliner.defaults = {    color: 'blue',    width: 1  }; }(jQuery));

Friday, February 8, 13

Page 25: Writing Reusable Web Components with jQuery and jQuery UI

Basic Plugin Review

• Protect The $

• Add Your Plugin To $.fn

• Return this

• Add options and defaults

Friday, February 8, 13

Page 26: Writing Reusable Web Components with jQuery and jQuery UI

Plugin Lab

• Write a “Same Text” plugin

• Activated on input elements

• When a user changes text in one input, text should be copied to other inputs

• Add validation regexp as an option. Copy only if text matches validation

Friday, February 8, 13

Page 27: Writing Reusable Web Components with jQuery and jQuery UI

Q & A

Friday, February 8, 13

Page 28: Writing Reusable Web Components with jQuery and jQuery UI

DOM ManipulatingPlugins

Friday, February 8, 13

Page 29: Writing Reusable Web Components with jQuery and jQuery UI

Simple Autocomplete

• Let’s write a simple autocomplete plugin

Friday, February 8, 13

Page 30: Writing Reusable Web Components with jQuery and jQuery UI

Solution Outline

• Create The New List Element

• Bind Event Handlers

var list_el = $('<ul class="autocomplete"></ul>'); list_el.on('click', 'li', function(ev) {// type code here}); self.on('input', function(ev) {// type code here});

Friday, February 8, 13

Page 31: Writing Reusable Web Components with jQuery and jQuery UI

Autocomplete Takeaways

• A plugin can use accompanying CSS file

• A plugin can “bind” event handlers

• Can maintain state using closures

Friday, February 8, 13

Page 32: Writing Reusable Web Components with jQuery and jQuery UI

Plugin Limitations

• Hard to maintain state

• Hard to destroy

• Hard to extend

Friday, February 8, 13

Page 33: Writing Reusable Web Components with jQuery and jQuery UI

Meet jQuery UIExtensible UI Library Build On Top of jQuery

Friday, February 8, 13

Page 34: Writing Reusable Web Components with jQuery and jQuery UI

What’s In The Box

• UI Framework

• UI Components

• Themes Framework

Friday, February 8, 13

Page 35: Writing Reusable Web Components with jQuery and jQuery UI

jQuery UI Core

WidgetsFactory

Autocomplete Widget

Tabs Widget

Gallery Widget

Enhanced jQuery Plugin

Friday, February 8, 13

Page 36: Writing Reusable Web Components with jQuery and jQuery UI

Our First Widget$(function($) {   $.widget('my.filler', {    _create: function() {      this.element.html('Hello World');    }  });}(jQuery));

// use the widget like a pluginvar widget = $('div').filler();

Friday, February 8, 13

Page 37: Writing Reusable Web Components with jQuery and jQuery UI

jQUI Basics

• A widget is an object (not a function)

• _create is the entry point

• this.element is the DOM element

• widget name is namespaced

Friday, February 8, 13

Page 38: Writing Reusable Web Components with jQuery and jQuery UI

Widget Lifecycle

• _create is called on creation

• _init is the default method

• _destroy terminates the widget

Friday, February 8, 13

Page 39: Writing Reusable Web Components with jQuery and jQuery UI

Fixing Our First Widget$(function($) {   $.widget('my.filler', {    _create: function() {      this.data = { previousContent: this.element.html() }      this.element.html('Hello World');    },    _destroy: function() {      this.element.html( this.data.previousContent );    }  });}(jQuery));

// later in codewidget.filler('destroy');

Friday, February 8, 13

Page 40: Writing Reusable Web Components with jQuery and jQuery UI

Widget Customizations

• A jQuery UI Widget can take options (just like with plugins)

• Options are automatically merged for you in the factory

• Can respond “live” to changes

Friday, February 8, 13

Page 41: Writing Reusable Web Components with jQuery and jQuery UI

Start With The Defaults$(function($) {   $.widget('my.filler', {    options: {      text: 'Hello World'    },     _create: function() { ...      this.element.html( this.option('text') );    },     _destroy: function() { ... }  });  }(jQuery));

Friday, February 8, 13

Page 42: Writing Reusable Web Components with jQuery and jQuery UI

Create Customized

• Pass the options when creating your widget

• No need to pass all.

var widget = $('div').filler({  text: 'Customization Is Cool'});

Friday, February 8, 13

Page 43: Writing Reusable Web Components with jQuery and jQuery UI

Changing Options• A user can get or set options at runtime

• Use the public method option

var widget = $('div').filler({  text: 'Customization Is Cool'}); console.log( widget.filler('option', 'text') );widget.filler('option', 'text', 'Bye bye');console.log( widget.filler('option', 'text') );

Friday, February 8, 13

Page 44: Writing Reusable Web Components with jQuery and jQuery UI

Changing Options• A user can get or set options at runtime

• Use the public method option

var widget = $('div').filler({  text: 'Customization Is Cool'}); console.log( widget.filler('option', 'text') );widget.filler('option', 'text', 'Bye bye');console.log( widget.filler('option', 'text') );

Widget Name Method Name

Friday, February 8, 13

Page 45: Writing Reusable Web Components with jQuery and jQuery UI

Handling Changes

• A widget can detect option change and respond to them by implementing _setOption method

• Remember to call super

_setOption: function(key, val) {  this._superApply(arguments);   this.element.html( this.option('text') );}, 

Friday, February 8, 13

Page 46: Writing Reusable Web Components with jQuery and jQuery UI

Other Public Methods

• Every method of your object which starts with a letter is public

• Call it from the outside using the widget

widget.filler('doSomething')

Friday, February 8, 13

Page 47: Writing Reusable Web Components with jQuery and jQuery UI

jQuery UI Events• Use _trigger to

trigger a custom event

• Arguments:

• event name

• event object

• hash data

_create: function() {  var self = this;

  setTimeout(function() {    self._trigger('done');  }, 2000);

},

Friday, February 8, 13

Page 48: Writing Reusable Web Components with jQuery and jQuery UI

jQuery UI Events

• Can bind event handlers

• Better yet - pass callbacks as options

var widget = $('div').filler({  done: function() {    console.log('Done !');  }});

Friday, February 8, 13

Page 49: Writing Reusable Web Components with jQuery and jQuery UI

Extending Widgets

• Extend a widget by passing it as the second argument to widget function

$.widget('my.complex', $.my.simple, {  // code goes here}); 

Friday, February 8, 13

Page 50: Writing Reusable Web Components with jQuery and jQuery UI

Q & A

Friday, February 8, 13

Page 51: Writing Reusable Web Components with jQuery and jQuery UI

Lab1

• Write a Progress Bar Widget

• Allow a user to change progress value

• Bonus: Use HTML Progress Element when supported

Friday, February 8, 13

Page 52: Writing Reusable Web Components with jQuery and jQuery UI

Lab2

• Write a plugin to limit characters in an input field

• Should show how many chars left, and prevent going beyond the limit

Friday, February 8, 13

Page 53: Writing Reusable Web Components with jQuery and jQuery UI

Selected PluginsjQuery UIOther jQuery Plugins

Friday, February 8, 13

Page 54: Writing Reusable Web Components with jQuery and jQuery UI

jQuery UI Widgets

• Demo page + Documentation:http://jqueryui.com/demos/

Friday, February 8, 13

Page 55: Writing Reusable Web Components with jQuery and jQuery UI

Equalize

http://tsvensen.github.com/equalize.js/

Friday, February 8, 13

Page 56: Writing Reusable Web Components with jQuery and jQuery UI

Gridster

http://gridster.net/

Friday, February 8, 13

Page 57: Writing Reusable Web Components with jQuery and jQuery UI

Zoomoozhttp://janne.aukia.com/zoomooz/

Friday, February 8, 13

Page 58: Writing Reusable Web Components with jQuery and jQuery UI

dd Slick

http://designwithpc.com/Plugins/ddSlick

Friday, February 8, 13

Page 59: Writing Reusable Web Components with jQuery and jQuery UI

jQuery Complexityhttp://danpalmer.me/jquery-complexify/

Friday, February 8, 13

Page 61: Writing Reusable Web Components with jQuery and jQuery UI

Filtering Content

http://luis-almeida.github.com/filtrify/

Friday, February 8, 13

Page 62: Writing Reusable Web Components with jQuery and jQuery UI

Fresco

http://www.frescojs.com/

Friday, February 8, 13

Page 63: Writing Reusable Web Components with jQuery and jQuery UI

Responsive Sliderhttp://responsive-slides.viljamis.com/

Friday, February 8, 13

Page 64: Writing Reusable Web Components with jQuery and jQuery UI

Trunk8http://jrvis.com/trunk8/

Friday, February 8, 13

Page 65: Writing Reusable Web Components with jQuery and jQuery UI

Tooltipsterhttp://calebjacob.com/tooltipster/

Friday, February 8, 13

Page 66: Writing Reusable Web Components with jQuery and jQuery UI

Page Slide

http://srobbin.com/jquery-plugins/pageslide/

Friday, February 8, 13

Page 67: Writing Reusable Web Components with jQuery and jQuery UI

Data Tableshttp://www.datatables.net/

Friday, February 8, 13

Page 68: Writing Reusable Web Components with jQuery and jQuery UI

Masonryhttp://masonry.desandro.com/

Friday, February 8, 13

Page 69: Writing Reusable Web Components with jQuery and jQuery UI

What Next

• Find more plugins at:http://plugins.jquery.com/

• Build your own plugins and components

Friday, February 8, 13

Page 70: Writing Reusable Web Components with jQuery and jQuery UI

Thanks For Listening

• Ynon Perek

• Talk to me: [email protected]

• Find more slides:http://ynonperek.com

Friday, February 8, 13