jquery - Create var's automatically from json key,value pairs for global use -
update: remembered can store them jquery(document).data(key,value); later use, idea ? expensive option performance-wise ?
i trying create var's automatically json array key,value pairs later use.
so created 2 sets of settings, 1- default settings (defsets) 2- user settings (usrsets). idea use user setting if exists, otherwise fallback default setting. not want write in vars 1 one, there way create global vars these arrays later use in various places ?
sth along lines like: var [key_name] = [key_value_read_from_user_setting_or_default_set];
here actual code below; problem is, seems create vars within each loop, cant access them globally think.
jquery(document).ready(function(e) { // read default settings array cookie if exist, otherwise create initial default settings , save if (jquery.cookie("defsets") != null) { var defsets = jquery.parsejson(jquery.cookie("defsets")) } else { var defsets = { 'ord': 'true', 'itemc': 4, 'ratio': 'sq', 'sortby': 'def'}; jquery.cookie("defsets", json.stringify(defsets)); }; // read user settings array cookie if exist, otherwise create initial empty user settings , save if (jquery.cookie("usrsets") != null) { var usrsets = jquery.parsejson(jquery.cookie("usrsets")) } else { var usrsets = {}; jquery.cookie("usrsets", json.stringify(usrsets)); } jquery.each(defsets, function(key, value){ // check every default setting if (usrsets[key] != undefined) { // if user setting exists respective default setting use var key = usrsets[key]; // uval = usrsets[key]; // dval = defsets[key]; // console.log("user have setting -" + key + "- value (" + uval + ") used instead of default: " + dval); } else { var key = defsets[key]; // console.log ("no user setting -" + key + "- use default val of :" + dval); }; });
after these, ideally, i'd able use var's anywhere below these lines, assuming var ratio has been created above; jquery(".selector").height(jquery(this).width * ratio);
if storing info globally need i'dd use namespaces. think better, after matter of preference, guess
Comments
Post a Comment