As a followup to http://stackoverflow.com/questions/3558240 I'm now having troubles with defining setters in a loop.
This is the code that I'm attempting to use within a Greasemonkey script.
var setValue_InStoredObject = function(_prefName, _varName, _newValue)
{
console.info('setValue_InStoredObject:\narguments = ');
console.info(arguments);
var tmp_original = JSON.parse(GM_getValue(_prefName, JSON.stringify(defaultColumnPrefixes)));
console.info(JSON.stringify(tmp_original));
tmp_original[_varName] = _newValue;
console.info(JSON.stringify(tmp_original));
GM_setValue(_prefName,JSON.stringify(tmp_original));
};
var getValue_FromStoredObject = function(_prefName, _varName, _defaultValue) {
return function() {
return JSON.parse(GM_getValue(_prefName, JSON.stringify(_defaultValue)))[_varName];
};
};
function addSettersGetters(_obj, _prefName,_defaults)
{
console.info(arguments);
for(var loop_columnName in _defaults)
{
var getfunc = getValue_FromStoredObject(_prefName,loop_columnName, _defaults);
_obj.__defineGetter__(loop_columnName, getfunc);
var setfunc = function(_newValue) { setValue_InStoredObject(_prefName,loop_columnName, _newValue) };
_obj.__defineSetter__(loop_columnName, setfunc);
}
}
var defaultColumnPrefixes = {
"flag": " | ",
"refName": " ",
"refSince": " ",
"nextPayment": " ",
"lastClick": " ",
"totalClicks": " ",
"average": " ",
"clickText": " ",
"average_1": " ",
"average_2": " ",
"RSA": " ",
"SD": " ",
"profit": "$"
};
var prefs = new function()
{
// Referral listings. Column preferences
this.columnPrefix = {};
addSettersGetters(this.columnPrefix, 'columnPrefix',defaultColumnPrefixes);
// Some logging to test whether the getters/setters work
// note: getters appear to work, though setters appear to have the same problem
// as those encountered when attempting to define the getters
console.info('this.columnPrefix.flag = '+ this.columnPrefix.flag);
console.info('this.columnPrefix.profit = '+ this.columnPrefix.profit);
console.group();
console.info('---');
// set the flag prefix to something (usually) different each time
this.columnPrefix.flag = new Date().getMilliseconds();
console.info('---');
console.groupEnd();
console.info('this.columnPrefix.flag = '+ this.columnPrefix.flag);
console.info('this.columnPrefix.profit = '+ this.columnPrefix.profit);
}
which produces the following output in Firebug:
this.columnPrefix.flag = |
this.columnPrefix.profit = $
---
setValue_InStoredObject: arguments =
["columnPrefix", "profit", 595]
{"flag":" | ","refName":" ","refSince":" ","nextPayment":" ","lastClick":" ","totalClicks":" ","average":" ","clickText":" ","average_1":" ","average_2":" ","RSA":" ","SD":" ","profit":"$"}
{"flag":" | ","refName":" ","refSince":" ","nextPayment":" ","lastClick":" ","totalClicks":" ","average":" ","clickText":" ","average_1":" ","average_2":" ","RSA":" ","SD":" ","profit":595}
---
this.columnPrefix.flag = |
this.columnPrefix.profit = 595
I suspect that this is caused by the same problem as my other question but I'm coming up against mind-blocks and cannot seem find a way to make it work.
Help please? =]