-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
80 lines (61 loc) · 1.64 KB
/
plugin.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
/**!
Plugin
Plugin description
@contributors:
@date-created:
*/
(function($) {
$.pluginName = function(el, options) {
// Default settings values
var defaults = {
parameter: '' // Parameter description
};
var plugin = this;
plugin.settings = {};
var $element = $(el),
element = el;
// Plugin initialization
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
updateSettingsFromHTMLData();
registerEvents();
};
// Reads plugin settings from HTML data-* attributes (camelCase)
var updateSettingsFromHTMLData = function() {
var data = $element.data();
for (var dat in data) plugin.settings[dat] = data[dat];
};
// Set event handlers on HTML elements (private method)
var registerEvents = function() {
/* $('.sub-elements',$element).on('click', function(e) {
e.preventDefault();
plugin.doSomething();
}); */
};
// Plugin do something (public method)
plugin.doSomething = function() {
/*
Do something with $element ?
and with plugin.settings.parameter ?
*/
};
// Destroy the plugin (public method)
plugin.destroy = function() {
/*
Remove event handlers
Reset styles or classes ?
*/
};
// Initialization
plugin.init();
};
$.fn.pluginName = function(options) {
return this.each(function() {
if (undefined === $(this).data('pluginName')) {
var plugin = new $.pluginName(this, options);
$(this).data('pluginName', plugin);
}
});
};
$('.js-plugin-elements').pluginName();
})(jQuery);