1

I'm working with the jQuery ColorPicker widget - specifically exercising the ColorPickerSetColor function (just 'setColor' internally). Code excerpt:

        setColor: function(col) {
            if (typeof col == 'string') {
                col = HexToHSB(col);
            } else if (col.r != undefined && col.g != undefined && col.b != undefined) {
                col = RGBToHSB(col);
            } else if (col.h != undefined && col.s != undefined && col.b != undefined) {
                col = fixHSB(col);
            } else {
                return this;
            }
            return this.each(function(){
                if ($(this).data('colorpickerId')) {
                    var cal = $('#' + $(this).data('colorpickerId'));
                    cal.data('colorpicker').color = col;
                    cal.data('colorpicker').origColor = col;
                    fillRGBFields(col, cal.get(0));
                    fillHSBFields(col, cal.get(0));
                    fillHexFields(col, cal.get(0));
                    setHue(col, cal.get(0));
                    setSelector(col, cal.get(0));
                    setCurrentColor(col, cal.get(0));
                    setNewColor(col, cal.get(0));
                }
            });
        }

It seems that there is a bug in the widget. The 'col' parameter, when inspected inside of the each() call, is undefined. I've read the documentation and other examples, and everything I can find indicates that 'col' should still be in scope when the each() call executes the function, but it doesn't seem to be...

Help?

Thanks!

rinogo
  • 8,491
  • 12
  • 61
  • 102
  • Are you sure col is defined before the return this.each is reached? – seth Sep 09 '09 at 00:54
  • Yes, I'm sure. If we add a console.log (Firebug's logging command) right before and right after the return this.each, the first log() command outputs a valid value, while the second only outputs undefined... – rinogo Sep 09 '09 at 16:57

2 Answers2

0

Try defining another variable:

 setColor: function(xCol) {
     var col = xCol;
     // ...
 }

If that works, then there's a weirdness in the closure system when it comes to parameters to functions. That sort of behavior could be browser-specific.

John Fisher
  • 22,355
  • 2
  • 39
  • 64
  • Hi John! Thanks for your answer. Unfortunately, it didn't work... Any other thoughts? – rinogo Sep 09 '09 at 16:57
  • Try setting the function to a variable before the return statement, then passing that variable as the parameter to "return this.each(f);" If that doesn't help, you'll need to post more of the code. – John Fisher Sep 09 '09 at 17:48
0

I just ended up using a temporary variable in a known-good scope (the parent of the setColor function). Definitely a hack, but it works. If anyone knows how to fix this the right way, please let me know. :)

Thanks! -Rich

rinogo
  • 8,491
  • 12
  • 61
  • 102
  • If you have the time and desire, you should write up a very minimal piece of code that duplicates the issue. In the process of doing this, you may even learn why it's happening. If you do, please post it here. – John Fisher Sep 10 '09 at 13:45