Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulate jQuery plugin defaults from json file
    primarykey
    data
    text
    <p>I'm building a jQuery plugin, that allows users to use custom themes, called as options like so:</p> <pre><code>$('element').myFunction({ theme: '/path/to/theme' }); </code></pre> <p>The theme contains a file <code>theme-info.json</code>, which contains the name of the theme, as well as some other metadata, and is read by the plugin when it is initalised:</p> <pre><code>{ "name": "themeName", "author": "authorName" } </code></pre> <p>What I'd like to be able to do, is populate the default options for the plugin, with an array of plugin options from the json file within the theme, so that they can be overridden when calling the function, as though they had been hardcoded like so:</p> <pre><code>$.fn.myFunction = function (options) { var defaults = { theme: '/default/theme/path', themeOptions: { // values called from json option1: 'value1', option2: 'value2' } } } </code></pre> <p>and then called with the plugin:</p> <pre><code>$('element').myFunction({ theme: '/path/to/theme', themeOptions: { option1: 'value1', option2: 'value2' } }); </code></pre> <p>Is it actually possible to read a JSON file into the defaults in this way? And if so, how is it done?</p> <p><strong>UPDATE</strong></p> <p>Playing with it a bit further, I've managed to get <em>something</em> returned, but I'm struggling to do anything with it. Here's the AJAX call to get the theme metadata and options:</p> <pre><code>$.ajax({ url: defaults.theme + 'theme-info.json', type: 'GET', dataType: 'json', async: false, success: function(data) { themeName = data.name; var data = $.extend(true, defaults, data); var options = $.extend(true, defaults, options); } }); </code></pre> <p>I then use </p> <pre><code>act.prepend('&lt;script&gt; var parts = \'[' + JSON.stringify(defaults.themeOptions) + ']\'; var themeOptions = $.parseJSON(parts); var themeOptions = themeOptions[0]; &lt;/script&gt;'); </code></pre> <p>Which prints the following at the top of the target container</p> <pre><code>var parts = '[{"backgroundColor":"#FF5859","testVar":"Tada!"}]'; var themeOptions = $.parseJSON(parts); var themeOptions = themeOptions[0]; </code></pre> <p>Then in the theme, I can use the variable <code>themeOptions.backgroundColor</code> which loads the values from the JSON brilliantly. However, the user defined options when calling the plugin are not overwriting the default values from the JSON. Any ideas?</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.
 

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