forked from jnicol/jquery-plugin-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-plugin-boilerplate-with-comments.js
145 lines (130 loc) · 4.1 KB
/
jquery-plugin-boilerplate-with-comments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* A jQuery plugin boilerplate.
* Author: Jonathan Nicol @f6design
*/
;(function($) {
// Change this to your plugin name.
var pluginName = 'demoplugin';
/**
* Plugin object constructor.
* Implements the Revealing Module Pattern.
*/
function Plugin(element, options) {
// References to DOM and jQuery versions of element.
var el = element;
var $el = $(element);
// Extend default options with those supplied by user.
options = $.extend({}, $.fn[pluginName].defaults, options);
/**
* Initialize plugin.
*/
function init() {
// Add any initialization logic here...
hook('onInit');
}
/**
* Example Public Method
*/
function fooPublic() {
// Code goes here...
}
/**
* Get/set a plugin option.
* Get usage: $('#el').demoplugin('option', 'key');
* Set usage: $('#el').demoplugin('option', 'key', value);
*/
function option (key, val) {
if (val) {
options[key] = val;
} else {
return options[key];
}
}
/**
* Destroy plugin.
* Usage: $('#el').demoplugin('destroy');
*/
function destroy() {
// Iterate over each matching element.
$el.each(function() {
var el = this;
var $el = $(this);
// Add code to restore the element to its original state...
hook('onDestroy');
// Remove Plugin instance from the element.
$el.removeData('plugin_' + pluginName);
});
}
/**
* Callback hooks.
* Usage: In the defaults object specify a callback function:
* hookName: function() {}
* Then somewhere in the plugin trigger the callback:
* hook('hookName');
*/
function hook(hookName) {
if (options[hookName] !== undefined) {
// Call the user defined function.
// Scope is set to the jQuery element we are operating on.
options[hookName].call(el);
}
}
// Initialize the plugin instance.
init();
// Expose methods of Plugin we wish to be public.
return {
option: option,
destroy: destroy,
fooPublic: fooPublic
};
}
/**
* Plugin definition.
*/
$.fn[pluginName] = function(options) {
// If the first parameter is a string, treat this as a call to
// a public method.
if (typeof arguments[0] === 'string') {
var methodName = arguments[0];
var args = Array.prototype.slice.call(arguments, 1);
var returnVal;
this.each(function() {
// Check that the element has a plugin instance, and that
// the requested public method exists.
if ($.data(this, 'plugin_' + pluginName) && typeof $.data(this, 'plugin_' + pluginName)[methodName] === 'function') {
// Call the method of the Plugin instance, and Pass it
// the supplied arguments.
returnVal = $.data(this, 'plugin_' + pluginName)[methodName].apply(this, args);
} else {
throw new Error('Method ' + methodName + ' does not exist on jQuery.' + pluginName);
}
});
if (returnVal !== undefined){
// If the method returned a value, return the value.
return returnVal;
} else {
// Otherwise, returning 'this' preserves chainability.
return this;
}
// If the first parameter is an object (options), or was omitted,
// instantiate a new instance of the plugin.
} else if (typeof options === "object" || !options) {
return this.each(function() {
// Only allow the plugin to be instantiated once.
if (!$.data(this, 'plugin_' + pluginName)) {
// Pass options to Plugin constructor, and store Plugin
// instance in the elements jQuery data object.
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
}
};
// Default plugin options.
// Options can be overwritten when initializing plugin, by
// passing an object literal, or after initialization:
// $('#el').demoplugin('option', 'key', value);
$.fn[pluginName].defaults = {
onInit: function() {},
onDestroy: function() {}
};
})(jQuery);