-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-plugin.js
executable file
·43 lines (32 loc) · 1.05 KB
/
jquery-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
// jQuery Plugin / Extension Boilerplate
// Self invoking anonymous function expression so that we can have encapsulation and keeps
// our plugin compatible with jQuery.noConflict (for example if multiple libraries being used)
(function($) {
// The closure also gives us encapsulation (private and public methods & properties)
var _privateMethod = function() {
};
var $.fn.publicMethod: function(action, options) {
var defaults = {
defaultProperty: 'defaultValue'
};
// Extend plugin options from defaults
var options = $.extend(defaults, options);
// Do some stuff based on action - use this
switch(action) {
case 'someAction':
break;
case 'everyElementAction':
// Loop through collection of elements
this.each(function() {
// Do somsething with each element - use this
});
break;
default:
return;
}
// Return this to make custom jQuery method chainable
return this;
};
})(jQuery);
// Invoke custom jQuery method
$('selector').publicMethod('action0', { customOption: 'value' });