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: http://thinkof.net/notify/index.html 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: http://thinkof.net/notify/javascript/notify.js 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.onclick = function () {eval ("new "+notifyOutEffect+ " (''"+notifyIdPrefix+"''+ ''AlertOverlay'');"); return false;} What I wanted was this (using the values inside the object, not the globally defined ones): objAlertOverlay.onclick = function () {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 Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
I apologize for this incomplete response, as I don''t have time right now to look at the script in whole, but I did check out your page. In FF 1.5 your alerts are VERY slow. Based only on your email, I''m assuming that is due in large part to your usage of the eval() statement, and that you should really try to cut that out of your code. Very rarely is that a requirement, and it does execute slower than normal code. Other than that, like I said I apologize for not having more of an answer. If I get some time I''ll try to take a gander. ________________________________ From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Greg Militello Sent: Friday, February 03, 2006 12:51 PM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org 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: http://thinkof.net/notify/index.html 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: http://thinkof.net/notify/javascript/notify.js 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.onclick = function () {eval ("new "+notifyOutEffect+ " (''"+notifyIdPrefix+"''+ ''AlertOverlay'');"); return false;} What I wanted was this (using the values inside the object, not the globally defined ones): objAlertOverlay.onclick = function () {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) The information transmitted in this electronic mail is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Ryan, I don''t expect an answer, I just thought I''d ask. ;) If you get time, I''ll take whatever insight you can give. As far as the speed issue, I have been thinking about turning the Effects off by default as well as they are not really needed. eval() is always my enemy. But to be honest, I am not sure how I can get away without it and have the same functionality (dynamic effects) Thanks, -Greg On Feb 3, 2006, at 2:25 PM, Ryan Gahl wrote:> I apologize for this incomplete response, as I don’t have time > right now to look at the script in whole, but I did check out your > page. In FF 1.5 your alerts are VERY slow. Based only on your > email, I’m assuming that is due in large part to your usage of the > eval() statement, and that you should really try to cut that out of > your code. Very rarely is that a requirement, and it does execute > slower than normal code. > > > > Other than that, like I said I apologize for not having more of an > answer. If I get some time I’ll try to take a gander. > > > > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Greg Militello > Sent: Friday, February 03, 2006 12:51 PM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > 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: > > http://thinkof.net/notify/index.html > > > > 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: > > http://thinkof.net/notify/javascript/notify.js > > > > 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.onclick = function () {eval ("new "+notifyOutEffect > + " (''"+notifyIdPrefix+"''+ ''AlertOverlay'');"); return false;} > > > > What I wanted was this (using the values inside the > object, not the globally defined ones): > > objAlertOverlay.onclick = function () {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) > > > > The information transmitted in this electronic mail is intended > only for the person or entity to which it is addressed and may > contain confidential, proprietary, and/or privileged material. Any > review, retransmission, dissemination or other use of, or taking of > any action in reliance upon, this information by persons or > entities other than the intended recipient is prohibited. If you > received this in error, please contact the sender and delete the > material from all computers. > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Well, try this: objAlertOverlay.onclick = function () {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.onclick = function () { 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: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Greg Militello Sent: Friday, February 03, 2006 11:51 AM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org 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: http://thinkof.net/notify/index.html 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: http://thinkof.net/notify/javascript/notify.js 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.onclick = function () {eval ("new "+notifyOutEffect+ " (''"+notifyIdPrefix+"''+ ''AlertOverlay'');"); return false;} What I wanted was this (using the values inside the object, not the globally defined ones): objAlertOverlay.onclick = function () {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 Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Greg, Wow. Thanks.... there is a lot to read in there.... I''ll try and look into this in a bit. But it looks like you may have solve my issues. Woo hoo. You guys all rock, BTW. I signed up for this list and I keep gleaming more and more. Javascript OO is daunting at first, but it helps knowing there are resources like this one. -Greg (not used to using "Greg" more than once in an email) Militello On Feb 3, 2006, at 3:40 PM, Gregory Hill wrote:> Well, try this: > > > > objAlertOverlay.onclick = function () {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.onclick = function () { 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: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Greg Militello > Sent: Friday, February 03, 2006 11:51 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > 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: > > http://thinkof.net/notify/index.html > > > > 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: > > http://thinkof.net/notify/javascript/notify.js > > > > 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.onclick = function () {eval ("new "+notifyOutEffect > + " (''"+notifyIdPrefix+"''+ ''AlertOverlay'');"); return false;} > > > > What I wanted was this (using the values inside the > object, not the globally defined ones): > > objAlertOverlay.onclick = function () {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 > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs