This repository was archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
This repository was archived by the owner on Sep 14, 2021. It is now read-only.
Update pluginifier to cater for method calls prior to plugin construction. #6
Copy link
Copy link
Open
Description
A plugin of mine gets recreated from scratch at various times by calling it's destroy method and then it's constructor. I can't readily determine whether it exist so I blindly call $el.myplugin('destroy'); This caused problems with the original pluginifier code as it created the instance using the string 'destroy' as it's options. The update below prevents that and will simply ignore method calls to plugins that haven't be constructed.
return this.each( function() {
/* Retrieve the instance from $.data()
* Here your plugin is assumed to live in namespace.plugins.name
* Look in the samples folder for a namespaced example
*/
var instance = $.data( this, plugin.plugin_name );
if( !instance ){
// If no instance exists yet ensure that options is an object and not a string specifying a method call.
// ie. client is calling a plugin method before the plugin has been instantiated.
if ( typeof options === "object" )
instance = $.data( this, plugin.plugin_name , new plugin( this, options )._init() );
// create the instance, _init() it, and store that instance in $.data()
}else
//If the first arg is a string we assume you are calling a method inside the plugin instance
if( typeof options === "string" ){
//underscored methods are "private" (similar to jQuery UI's $.widget we allow this to make methods not availble via public api)
options = options.replace( /^_/ , "" );
// Check if underscore filtered method exists
if( instance[options] ) {
// Call method with args
instance[options].apply( instance, args );
}else{
console.log( 'Plugin: ' + plugin.plugin_name + ' doesn\'t have a method: ' + options );
}
}
});Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels