How to call public method jquery plugin from outside? -
i read lot undertand not working.
i'm using jquery plugin boilerplate.
i have plugin called "defaultpluginname" , have public method inside plugin:
sayhey: function(){ alert('hey!'); }
i want call method outsise, that:
$('#test').defaultpluginname('sayhey');
but not working, follow here fidle link give better view issue: http://jsfiddle.net/tcxwj/
the method trying call not public need alter plugin in order trigger outside plugin.
see here how can that: https://github.com/jquery-boilerplate/jquery-boilerplate/wiki/another-extending-jquery-boilerplate.
basically need change
$.fn[ pluginname ] = function ( options ) { return this.each(function() { if ( !$.data( this, "plugin_" + pluginname ) ) { $.data( this, "plugin_" + pluginname, new plugin( this, options ) ); } }); };
to
$.fn[pluginname] = function ( arg ) { var args, instance; // allow plugin instantiated once if (!( this.data( 'plugin_' + pluginname ) instanceof plugin )) { // if no instance, create 1 this.data( 'plugin_' + pluginname, new plugin( ) ); } instance = this.data( 'plugin_' + pluginname ); /* * because boilerplate support multiple elements * using same plugin instance, element should set here */ instance.element = this; // first parameter object (arg), or omitted, // call plugin.init( arg ) if (typeof arg === 'undefined' || typeof arg === 'object') { if ( typeof instance['init'] === 'function' ) { instance.init( arg ); } // checks requested public method exists } else if ( typeof arg === 'string' && typeof instance[arg] === 'function' ) { // copy arguments & remove function name args = array.prototype.slice.call( arguments, 1 ); // call method return instance[arg].apply( instance, args ); } else { $.error('method ' + arg + ' not exist on jquery.' + pluginname); } };
then pick method.
hope helps.
Comments
Post a Comment