Populate jQuery plugin defaults from json file -
i'm building jquery plugin, allows users use custom themes, called options so:
$('element').myfunction({ theme: '/path/to/theme' });
the theme contains file theme-info.json
, contains name of theme, other metadata, , read plugin when initalised:
{ "name": "themename", "author": "authorname" }
what i'd able do, populate default options plugin, array of plugin options json file within theme, can overridden when calling function, though had been hardcoded so:
$.fn.myfunction = function (options) { var defaults = { theme: '/default/theme/path', themeoptions: { // values called json option1: 'value1', option2: 'value2' } } }
and called plugin:
$('element').myfunction({ theme: '/path/to/theme', themeoptions: { option1: 'value1', option2: 'value2' } });
is possible read json file defaults in way? , if so, how done?
update
playing bit further, i've managed something returned, i'm struggling it. here's ajax call theme metadata , options:
$.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); } });
i use
act.prepend('<script> var parts = \'[' + json.stringify(defaults.themeoptions) + ']\'; var themeoptions = $.parsejson(parts); var themeoptions = themeoptions[0]; </script>');
which prints following @ top of target container
var parts = '[{"backgroundcolor":"#ff5859","testvar":"tada!"}]'; var themeoptions = $.parsejson(parts); var themeoptions = themeoptions[0];
then in theme, can use variable themeoptions.backgroundcolor
loads values json brilliantly. however, user defined options when calling plugin not overwriting default values json. ideas?
have jquery.getjson
with method u can read json file on fly.
Comments
Post a Comment