0

As a followup to Setting javascript defineGetter and defineSetter in a loop 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? =]

Community
  • 1
  • 1
kwah
  • 1,149
  • 1
  • 13
  • 27

1 Answers1

1

Try setting var setFunc out of the for loop.

var setFunction = function(_prefName,loop_columnName){
    return function(_newValue){
        setValue_InStoredObject(_prefName,loop_columnName, _newValue)
    }
};

Then in the loop:

var setfunc = setFunction(_prefName,loop_columnName);
_obj.__defineSetter__(loop_columnName, setfunc);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • No luck with this - the setter function (setfunc) expects exactly one variable (the value being assigned). Using the code exactly as you suggested throws the error '_newValue' is undefined, and wrapping it in a function does not work (same result with/without 'return'). The problem appears to be that 'loop_columnName' keeps being set to the last value in the loop - the closure issue if I'm not mistaken? – kwah Aug 25 '10 at 20:30
  • AWESOME! On first testing it appears to be working perfectly =] I still need to look at it closer before I can understand exactly what is happening though. – kwah Aug 26 '10 at 14:41
  • You said the setter function takes one argument, `_newValue`. So, what I did was make a function, that returns a function that takes one parameter (`_newValue`). Since this function is outside of the loop, it correctly closes around the `_prefName` and `loop_columnName` variables. – gen_Eric Aug 26 '10 at 15:15