My plugin allows you to specify a callback in its options. The plugin may run before the DOM is ready, but I need to make sure the callback only runs once the DOM is ready, so I'm wrapping the callback.call()
within a $( document ).ready()
handler.
The problem is I want to maintain the plugin's context within the callback. In other words, within the callback, I want this
to be the plugin, not document
.
This is what I've come up with, but I'm not sure it's really the best way.
function MyPlugin( element, options ){
this.element = element;
this.options = $.extend( {}, defaults, options );
MyPlugin.prototype = {
functionWithCallback: function(){
if ( typeof this.options.callback == 'function' )
$( document ).ready( function( plugin ){ plugin.options.callback.call( plugin ) }( this )
}
}
}
That last line is ugly, but it allows me to pass the plugin's this
context to the callback.
I would rather do $( document ).ready( this.options.callback )
and be done with it, but then within the callback, this
is document
which doesn't really help, and doesn't necessarily give me easy access to the element the plugin was called on...
Is there a better way?