Well, try this:

 

objAlertOverlay. () {eval ("new "+this.options.outEffect+ " ('"+ this.options.idPrefix+"'+ 'AlertOverlay');"); return false;}.bind(this);
 
That should bind your function to the ‘this’ object, so you can then reference any values from ‘this’.
 
As for avoiding eval, you can change this.options.notifyOutEffect to be a function, in your initialize function:

var options = Object.extend({
                       messagecolor:          "#ffff99",
                       inEffect:                      "Effect.Appear",
                       outEffect:                     function (el) { new Effect.Appear(el) },
                       idPrefix:                      "Notify",
                       autoHide:                      "false"
               }, arguments[1] || {});
 
Then do:
 
objAlertOverlay. () { this.options.outEffect(this.options.idPrefix + 'AlertOverlay'); return false; }.bind(this);
 
Also, you should probably use the prototype event binding syntax, as to avoid browser incompatibilities:

Event.observe(objAlertOverlay, ‘click’, function () { this.options.outEffect(this.options.idPrefix+ 'AlertOverlay'); return false; }.bind(this));
 
 
Also, I believe this would actually work, because _javascript_ seems to support closures (at least it seems to from my own experiences):

Event.observe(objAlertOverlay, ‘click’, function () { this.options.outEffect(objAlertOverlay); return false; }.bind(this));
 
Notice that I just pass the object into the bound function, instead of recreating the id of the element.  It *should* work that way, but I may have just been lucky in my own experiences.
 
I apologize in advance if newlines get removed in this email. I have to use Outlook at work, and it’s retarded.
 
Greg

 

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Greg Militello
Sent: Friday, February 03, 2006 11:51 AM
To: [email protected]
Subject: [Rails-spinoffs] Anonymous function variable scope issue

 

Hey all,

          I joined this list mostly because I don't see too much in the OO _javascript_ arena, and I have q's (of course).

 

I have a little pet project I am working on, you can see it here:

 

          I have a class Notify.Alert() that takes some options and displays a div alert instead of a _javascript_ one.  Currently it is configurable to allow several options, including the effects for showing and hiding the divs (using script.aculo.us).  

 

          Now onto the questions.  If you look at my main _javascript_ include:

 

          You should notice the first declarations I have are:

var notifyDestination = '';

var notifyOutEffect = '';

var notifyIdPrefix = '';

 

          The only reason these exist is because of variable scope issues.  If you look at Notify.Alert.draw()  you'll see that I am using them in an anonymous function for an onClick event:

objAlertOverlay. () {eval ("new "+notifyOutEffect+ " ('"+notifyIdPrefix+"'+ 'AlertOverlay');"); return false;}

 

          What I wanted was this (using the values inside the object, not the globally defined ones):

objAlertOverlay. () {eval ("new "+this.options.notifyOutEffect+ " ('"+this.options.notifyIdPrefix+"'+ 'AlertOverlay');"); return false;}

          But IE chokes, and says it doesn't know how to implement it (FireFox/Safari work).   I think I need the eval there, or else the objAlertOverlay won't know the Effect class, etc.

 

 

          I also don't like my solution (even if it does work).  I was thinking an event listener or something may help, but honestly I don't have a clue how to implement one.  Any thoughts?  Is there an easy way to do this that I just am not seeing?

 

 

Thanks a ton, sorry if it was long winded.  Feel free to ask questions if I was not clear enough.

 

-Greg

 

PS: If you feel like giving me feedback on the script itself, I would appreciate that too.  I do not mind other peoples input.  (I have a list of todo's already)

 

_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to