I'm using jQuery's event system to allow external code to drive my plugin. In my event handlers 'this' is set to the element that the events are bound to, so what's the best way for me to access the plugin's methods themselves?
;(function($, window, document, undefined){
var pluginName = "book";
// Standard constructor
function Plugin(element, options){
this.element = element;
this.options = $.extend({}, defaults, options);
this.init();
}
// Simple init
Plugin.prototype.init = function(){
this.setBindings();
}
// Tie local methods to event channels
// so that external code can drive the plugin.
Plugin.prototype.setBindings = function(){
var events = {
'book-open' : this.open,
'book-next-page' : this.toNext,
'book-prev-page' : this.toPrev,
'book-cover' : this.toFront,
'book-back' : this.toBack
}
for(event in events){
var fn = events[event];
console.log(event);
this.$element.on(event, fn);
}
};
// Event Handlers
Plugin.prototype.open = function(){
// when called externally 'this' refers
// to the element the plugin was intialized on.
// I want to be able to call the plugin's 'private'
// methods, like someMethod() below.
};
/* .... other event handlers ... */
// 'Private' plugin methods
Plugin.prototype.someMethod = function(){
// do something
}
// Wrap and return technique from @ajpiano & @addyosmani
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if ( !$.data(this, "plugin_" + pluginName )) {
$.data( this, "plugin_" + pluginName,
new Plugin( this, options ));
}
});
}
})(jQuery, window, document);