Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting jQuery plugin that accepts method and options
    primarykey
    data
    text
    <p>I am following the instructions <a href="http://docs.jquery.com/Plugins/Authoring" rel="nofollow">for authoring a plugin</a> from the jQuery docs. I am trying to keep my calls in the same namespace as they instruct in section 6.1 however I also need to be able to pass through more options with each call.</p> <p><em>What I'd like to do</em></p> <pre><code>$('#element').myFunction({ method: 'method1', option1: 'option1', option2: 'option2' }); </code></pre> <p><em>What I have currently</em></p> <pre><code>(function($) { var methods = { init: function(options) { //initialization }, method1: function(options) { var settings = $.extend({ 'option1' = 'option default', 'option2' = 'option 2 default' }, options); //use settings for given method ex: settings.option1, settings.option2 } }; $.fn.myFunction(options) { //method logic if(methods[options.method]) { return methods[options.method].apply(this, Array.prototype.slice.call(arguments, 1)); //I'm thinking I need to do something here to pass through the options to the methods??? } else if (typeof options.method === 'object' || !options.method) { return methods.init.apply(this, arguments); //or possibly here? } else { $.error('Method ' + options.method + ' does not exist on jQuery.myFunction'); } }; })(jQuery); </code></pre> <p>I've not been doing front end web development for sometime and am trying to brush back up on it, however the method logic section is somewhat confusing to me. I need to understand what processing is happening on the <code>methods[options.method].apply()</code>. I know this is where each method is being called but am just not sure as to where additional options would be passed.</p> <p><strong>[update1]</strong></p> <p>I have read some more on what's happening with the <code>apply()</code> and believe that it passes through the object and any other arguments. I've tried changing it to <code>methods[options.method].apply(this, options);</code> however this doesn't seem to have corrected my issue.</p> <p><strong>[update2]</strong></p> <p>I now have my code working by making the following changes</p> <pre><code> var methods = { init: function(options) { //initialization }, method1: function(element, options) { var settings = $.extend({ 'option1' = 'option default', 'option2' = 'option 2 default' }, options); //use settings for given method ex: settings.option1, settings.option2 element.each(function() { }; } }; $.fn.myFunction(options) { //method logic if(methods[options.method]) { return methods[options.method](this, options); // apply(this, Array.prototype.slice.call(arguments, 1)); //I'm thinking I need to do something here to pass through the options to the methods??? } else if (typeof options.method === 'object' || !options.method) { return methods.init.apply(this, options); // arguments); //or possibly here? } else { $.error('Method ' + options.method + ' does not exist on jQuery.myFunction'); } }; </code></pre> <p>I am going to leave this open for a few days though, anyone that wants to explain what the original code was trying to do vs. my changes I will accept that as the answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload