forked from jquery-boilerplate/jquery-patterns
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding mark dagleishs highly configurable pattern. updated, generaliz…
…ed and commented to make it easier to read
- Loading branch information
1 parent
f0fb750
commit eeb58a8
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 'Highly configurable' mutable plugin boilerplate | ||
* Author: @markdalgleish | ||
* Further changes, comments: @addyosmani | ||
* Licensed under the MIT license | ||
*/ | ||
|
||
|
||
// Note that with this pattern, as per Sexton's, the plugin logic | ||
// hasn't been nested inside a jQuery plugin. Instead, we just use | ||
// jQuery for the instantiation of it. | ||
|
||
;(function($, window, document, undefined){ | ||
|
||
// our plugin constructor | ||
var Plugin = function(elem, options){ | ||
this.elem = elem; | ||
this.$elem = $(elem); | ||
this.options = options; | ||
|
||
// this next line takes advantage of HTML5 data attributes | ||
// to support customization with the plugin on a per-element | ||
// basis. eg | ||
// <div class=item' data-plugin-options='{"message":"Goodbye World!"}'></div> | ||
this.metadata = this.$elem.data('plugin-options'); | ||
}; | ||
|
||
// the plugin prototype | ||
Plugin.prototype = { | ||
defaults: { | ||
message: 'Hello world!' | ||
}, | ||
|
||
init: function() { | ||
// Introduce defaults that can be extended either globally or using an | ||
// an object literal. | ||
this.config = $.extend({}, this.defaults, this.options, this.metadata); | ||
|
||
// Sample usage: | ||
// Set the message per instance: | ||
// $('#elem').plugin({ message: 'Goodbye World!'}); | ||
// or | ||
// var p = new Plugin(document.getElementById('elem'), { message: 'Goodbye World!'}).init() | ||
// or, set the global default message: | ||
// Plugin.defaults.message = 'Goodbye World!' | ||
|
||
this.sampleMethod(); | ||
return this; | ||
}, | ||
|
||
sampleMethod: function() { | ||
// eg. show the currently configured message | ||
// console.log(this.config.message); | ||
} | ||
} | ||
|
||
Plugin.defaults = Plugin.prototype.defaults; | ||
|
||
$.fn.plugin = function(options) { | ||
return this.each(function() { | ||
new Plugin(this, options).init(); | ||
}); | ||
}; | ||
|
||
//optional: window.Plugin = Plugin; | ||
|
||
})(jQuery, window); | ||
|