Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Plugin - Deep option modification
    primarykey
    data
    text
    <p>I am currently working a plugin with a settings variable that is fairly deep (3-4 levels in some places). Following the generally accepted jQuery Plugin pattern I have implemented a simple way for users to modify settings on the fly using the following notation:</p> <pre><code>$('#element').plugin('option', 'option_name', 'new_value'); </code></pre> <p>Here is the code similar to what I am using now for the options method.</p> <pre><code>option: function (option, value) { if (typeof (option) === 'string') { if (value === undefined) return settings[option]; if(typeof(value) === 'object') $.extend(true, settings[option], value); else settings[option] = value; } return this; } </code></pre> <p>Now consider that I have a settings variable like so:</p> <pre><code>var settings = { opt: false, another: { deep: true } }; </code></pre> <p>If I want to change the <code>deep</code> settings I have to use the following notation:</p> <pre><code>$('#element').plugin('option', 'another', { deep: false }); </code></pre> <p>However, since in practice my settings can be 3-4 levels deep I feel the following notation would be more useful:</p> <pre><code>$('#element').plugin('option', 'another.deep', false); </code></pre> <p>However I'm not sure how feasible this is, nor how to go about doing it. As a first attempt I tried to "traverse" to the option in question and set it, but if I set my traversing variable it doesn't set what it references in the original settings variable.</p> <pre><code>option: function (option, value) { if (typeof (option) === 'string') { if (value === undefined) return settings[option]; var levels = option.split('.'), opt = settings[levels[0]]; for(var i = 1; i &lt; levels.length; ++i) opt = opt[levels[i]]; if(typeof(value) === 'object') $.extend(true, opt, value); else opt = value; } return this; } </code></pre> <p>To say that another way: By setting <code>opt</code> after traversing, the setting it actually refers to in the <code>settings</code> variable is unchanged after this code runs.</p> <p>I apologize for the long question, any help is appreciated. Thanks!</p> <hr> <p><strong>EDIT</strong></p> <p>As a second attempt I can do it using <code>eval()</code> like so:</p> <pre><code>option: function (option, value) { if (typeof (option) === 'string') { var levels = option.split('.'), last = levels[levels.length - 1]; levels.length -= 1; if (value === undefined) return eval('settings.' + levels.join('.'))[last]; if(typeof(value) === 'object') $.extend(true, eval('settings.' + levels.join('.'))[last], value); else eval('settings.' + levels.join('.'))[last] = value; } return this; } </code></pre> <p>But I really would like to see if anyone can show me a way to <em>not</em> use eval. Since it is a user input string I would rather not run <code>eval()</code> on it because it could be anything. Or let me know if I am being paranoid, and it shouldn't cause a problem at all.</p>
    singulars
    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