-
-
Save ann71727/561ac5807d491984ccb06ff584ae66ec to your computer and use it in GitHub Desktop.
Js prototype with jquery plugin integration
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
<div id="mydiv" data-pluginname-options="{foo:'bar', buzz:2, icanhas:true}" data-pluginname-optionname="foo"> | |
</div> |
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
/** | |
* | |
*/ | |
(function ($) { | |
PluginName = function () { | |
this.init.apply(this, arguments); | |
}; | |
PluginName.prototype = { | |
constructor: PluginName.prototype.constructor, | |
options: { | |
}, | |
init: function (element, options) { | |
//main element | |
this.$element = element; | |
//set options | |
this.options = this.setOptions(options); | |
this.setElements(); | |
this.setEvents(); | |
}, | |
setElements: function () { | |
console.log('setElements'); | |
}, | |
setEvents: function () { | |
console.log('setEvents'); | |
}, | |
setOptions:function(options) { | |
var options = $.extend(true, {}, this.options, options); | |
//check options on the node | |
var pluginnameRe = new RegExp('^pluginName'); | |
var data = this.$element.data(); | |
for (var i in data) { | |
if(i.indexOf('pluginName')==0 && data.hasOwnProperty(i)) { | |
} | |
} | |
} | |
}; | |
//jquery plugin implementation | |
$.fn.pluginName = function(options) { | |
return this.each(function() { | |
if(!$(this).data('PluginName')) | |
$(this).data('PluginName', new PluginName(this, options)); | |
}); | |
}; | |
})(jQuery); |
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
//Init as jquery plugin | |
$('div').pluginName({ | |
'foo':'bar' | |
}); | |
//Init by using new | |
new PluginName($('div'), { | |
'foo':'bar' | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment