I'm creating a jQuery plugin and wish to offer several extensions to make by solution more modular. I think I've got the correct syntax to perform the extend, but I can't seems to call any of the methods in my extension. I've only included sections of the code to try and be brief but I can provide more if needed. Any help is much appreciated!
I'm using http://jqueryboilerplate.com/ for my main plugin and this stackoverflow answer for adding my extension methods: Best Way to Extend a jQuery Plugin
Main jQuery plugin
function Plugin( element, options ) {
this.element = element;
// jQuery has an extend method which merges the contents of two or
// more objects, storing the result in the first object. The first object
// is generally empty as we don't want to alter the default options for
// future instances of the plugin
this.options = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
//This line does NOT work
var id = this.showId();
console.log(id);
var toolbar = this.createToolbar(this.options);
$(this.element).append(toolbar);
if(this.options.showPalette)
{
var palette = this.createPalette(this.options);
$(this.element).append(palette);
this.fetchPalette();
}
var canvas = this.createCanvas(this.options);
$(this.element).append(canvas);
}, ....
Plugin constructor
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin( this, options ));
}
});
};
Extension method
var extensionMethods = {
/*
* retrieve the id of the element
* this is some context within the existing plugin
*/
showId: function(){
return this.element[0].id;
}, ....
jQuery extend
$.extend(true, $['fn']['workflowEditor'].prototype, extensionMethods);