Top Banner
jQuery Plugin Design Patterns “Deep into JS” Meetup
26

Plugin jQuery, Design Patterns

Aug 14, 2015

Download

Technology

Robert Casanova
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Plugin jQuery, Design Patterns

jQuery Plugin Design Patterns

“Deep into JS” Meetup

Page 2: Plugin jQuery, Design Patterns

Constructor Function

Page 3: Plugin jQuery, Design Patterns

function Person() {}

Page 4: Plugin jQuery, Design Patterns

function Person(name, surname) {!! ! ! this.name = name;!! ! ! this.surname = surname;!}

Page 5: Plugin jQuery, Design Patterns

function Person(name, surname) {!! ! ! this.name = name;!! ! ! this.surname = surname;!! ! ! this.getName = function() {!! ! ! ! ! ! // privileged method: useful if you need private method, attributes!

! ! ! ! ! ! return this.name;!! ! ! }!}

Page 6: Plugin jQuery, Design Patterns

function Person(name, surname) {!! ! ! this.name = name;!! ! ! this.surname = surname;!}!Person.prototype.getName = function() {!! ! ! ! return this.name;!}

Page 7: Plugin jQuery, Design Patterns

var robb = new Person(‘Robert’, ‘Casanova’);!console.log(robb.getName());

Page 8: Plugin jQuery, Design Patterns

Object.create()

Page 9: Plugin jQuery, Design Patterns

var robb = {! ! name: ‘Robert’,! ! surname: ‘Casanova’,!! ! getName: function() {! ! ! ! ! return this.name;! ! }!}

Page 10: Plugin jQuery, Design Patterns

var gigi = Object.create(robb);!gigi.name = ‘Giorgio’;!gigi.surname = ‘Moroder’;!!

console.log(gigi.getName())

Page 11: Plugin jQuery, Design Patterns

Plugin jQuery

Page 12: Plugin jQuery, Design Patterns

$.fn[‘pluginName’] = function() { this.each(function(){ $(this).doSomething(); });}

Simplest jQuery plugin ever

Page 13: Plugin jQuery, Design Patterns

LightWeight Start Pattern

Page 14: Plugin jQuery, Design Patterns

;(function($, window,document, undefined ){! //plugin goes here!})(jQuery, window, document)

Immediately Invoked Function Expression (IIFE)

Page 15: Plugin jQuery, Design Patterns

var pluginName = “mioPlugin”,! defaults = { defaultProperty: ‘defaultValue’ }

Defaults

Page 16: Plugin jQuery, Design Patterns

function Plugin(el, options) {!! this.el = el;! this.$el = $(el);! this.options = $.extend({},defaults, options);!! ! ! this._defaults = defaults;! this._name = pluginName;!!

! ! this.init();!}

Constructor Function

Page 17: Plugin jQuery, Design Patterns

Plugin.prototype.init = function() {!! ! ! ! //the initial logic goes here!}!Plugin.prototype.someMethod = function() {! …!}

Methods

Page 18: Plugin jQuery, Design Patterns

$.fn[pluginName] = function(options) {!! ! ! return this.each(function(){!! ! ! ! ! if(!$.data(this, pluginName)) {!! ! ! ! ! ! $.data(this, pluginName, new Plugin(this, options));!! ! ! ! ! }!! ! ! });!}

Plugin Magic

Page 19: Plugin jQuery, Design Patterns

$(‘#elem’).pluginName({! !! defaultProperty: ‘value’!});

Example

Page 20: Plugin jQuery, Design Patterns

DOM-to-Object Bridge Pattern

Page 21: Plugin jQuery, Design Patterns

var myObject = {!! ! init: function (options,elem) {!! ! ! ! …!!! ! }!}

Object

Page 22: Plugin jQuery, Design Patterns

init: function (options,elem) {!! ! this.options = $.extend({}, this.options,options);!! ! this.el = elem;! this.$el = $(elem);!!

this._build();!!

return this;!}

Init Function

Page 23: Plugin jQuery, Design Patterns

options: {!! defaultOption: ‘defaultValue’!}

Default options

Page 24: Plugin jQuery, Design Patterns

_build: function() {!! this.$el.html(‘inizialize html here’);!},!publicMethod: function() { this.$el.doSomething();}

Methods

Page 25: Plugin jQuery, Design Patterns

$.plugin = function(name,object) {!! ! $.fn[name] = function(options) {!! ! ! ! return this.each(function(){! ! ! ! ! ! if(!$.data(this,name)) { $.data(this,name, Object.create(myObject).init(options,this)); } })!! ! }!} !$.plugin(“pluginName”, myObject )

Plugin Magic

Page 26: Plugin jQuery, Design Patterns

$(‘#elem’).pluginName({! !! defaultProperty: ‘value’!});!!

var plugin = $(‘#elem’).data(‘pluginName’);!plugin.publicMethod();

Example