Friday 15 April 2011

I need a shorter access to my jQuery Plugin methods -


Perhaps a question has already been answered in this way, but after a while I will not find a definite answer Which matches this method

Then my plugin looks at its most basic level, I want to be able to do this once that an element has passed through this, a brief reference to this plugin's methods Might be able to, like something:

plugin on element:

  $ ('# myElement'). MyPlugin ({options});   

Then to use a method:

  $ ('# myElement'). MyPlugin ('myFirstMethod', {params});   

There is a plugin shell:

 ; (Function ($) {"strict use"; var plug-in = {}; var default = {myDefault: 'some_value'}; $ .fn.myplugin = function (option) {if (this.length & gt ; 1) {this.each (function () {$ (this) .myplugin (option);}); back it;} var myplugin = {}; var element = this; plugin. Element = it; var init = function () {Myplugin.settings = $ .extend ({}, default, option); // other init stuff here}; form .myfirstmethod = function () {// something}; form.mySecondMethod = function () {/ / Anything else}; init (); Returns it; // Returns the current jQuery object}}) (jQuery);   

Again, I'm sure the answer should be somewhere else. I need 'smallest' access to methods.

Thank you.

Personally I have to type pluginName ('method', 'params') I hate the ways to call because it does not give you the complete toolkit which provides JavaScript. Apart from this, I do not consider calling the same method for two different purposes. In your case, you want $ (). MyPlugin () to init and $ (). MyPlugin ("methodName") to execute a method. This is a method myPlugin () for two completely different purposes but if you want to roll in the way, I believe the following snippet should be sufficient.

  // plugin (function ($) {var myplugin = function (elm) {this.elm = $ (elm);}; myplugin.prototype.myFirstMethod = function () {console Log ("firstmethod")}} myplugin.prototype.mySecondMethod = function () {console.log ("secondmethod");} $ .fn.myplugin data ("myplugin")) {$ (this). Data ("myplugin", new myplugin (this (!));} Var api = $ (this) .data ("myplugin"); if (myArgs.length & gt; 0) {API [myArgs [0]] ( MyArgs [1]);}})}}}}) (jQuery); // init plugin $ (".foo"). Myplugin (); $ (".foo") myplugin ("myFirstMethod") .;   

If you want to execute this method on each element of a jquery collection, then you have to perform and execute a each .

Does that work?

No comments:

Post a Comment