Last active
November 6, 2016 14:21
-
-
Save cuonghuynh/586288420757fc677eae8fa8e8a83aa7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(function($) { | |
$.pluginName = function(element, options) { | |
var defaults = { | |
foo: 'bar', | |
onFoo: function() {} | |
} | |
var plugin = this; | |
plugin.settings = {} | |
var $element = $(element), | |
element = element; | |
plugin.init = function() { | |
plugin.settings = $.extend({}, defaults, options); | |
// code goes here | |
} | |
plugin.foo_public_method = function() { | |
// code goes here | |
} | |
var foo_private_method = function() { | |
// code goes here | |
} | |
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); | |
} | |
}); | |
} | |
})(jQuery); | |
=========================== | |
$(document).ready(function() { | |
// attach the plugin to an element | |
$('#element').pluginName({'foo': 'bar'}); | |
// call a public method | |
$('#element').data('pluginName').foo_public_method(); | |
// get the value of a property | |
$('#element').data('pluginName').settings.foo; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment