[Proto-Scripty] Re: Event.observer with mouseup?
Doesnt even need to be a function .. Event.observe(window, 'mouseup', function(event) { alert('foo '+ event); }); - Original Message - From: "T.J. Crowder" To: "Prototype & script.aculo.us" Sent: Saturday, January 03, 2009 5:50 PM Subject: [Proto-Scripty] Re: Event.observer with mouseup? > Also any reference to a function inside of an observer needs to be in > the form of an anonymous function, not a literal function itself. Doesn't have to be anonymous. Named functions are fine: function myMouseUpThingy() { alert('Howdy'); } // later Event.observe(window, 'mouseup', myMouseUpThingy); The critical thing is that you need to pass in a function *reference*. -- T.J. On Jan 3, 4:45 pm, Walter Lee Davis wrote: > Try doing this instead: > > //put the following in a script block near the bottom of your html > var foo = function(){ alert('howdy') }; > Event.observe(window,'mouseup',foo); > > Note that the function is called Event.observe, not Event.observer. > Also any reference to a function inside of an observer needs to be in > the form of an anonymous function, not a literal function itself. You > could have done this as well: > > Event.observe(window,'mouseup',function(){alert('howdy')}); > > It's a subtle distinction, but it becomes much more important in a non- > trivial example, because an anonymous function won't get its input > variables (and won't be called, either) until it is invoked. Then it > "uncloaks", does its business, and vanishes again. > > Walter > > On Jan 2, 2009, at 1:30 PM, Per wrote: > > > > > Hi, > > I'm new to protocoljs, but it's this thing I'm trying to do, and it > > doesn't work. It drives me mad. > > > Anyway, > > I try to do this with protocoljs: > > > > > This was how I wrote it, but it doesn't work: > > Event.observer(window,'mouseup',alert('hi')); > > > What is the right way to do it? > > > Thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Event.observer with mouseup?
On Jan 4, 10:21 am, Joe Athman wrote: > Another alternative is to use Prototype's Curry function which let's > you create new functions with default arguments. > > So you could write: > Event.observe(window, 'mouseup', alert.curry('hi')); `alert.curry("hi")` won't work in IE, where `window.alert` is not an instance of Function object and does not "inherit" from `Function.prototype` (as, pretty much, any other JScript host method). You can, of course, try to call `curry` from the `Function.prototype` directly (`Function.prototype.curry.call(window.alert, 1)`), but since `curry` internally tries to invoke `call` directly on a context object, this will fail once again. It would work if Prototype.js was invoking `call` from the `Function.prototype` rather than from the context object `(Function.prototype.call.call(alert, window, 1))` but it doesn't and it's not clear if such change is even worth the trouble. Never expect anything from host objects. > > Joe > [...] -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Event.observer with mouseup?
Another alternative is to use Prototype's Curry function which let's you create new functions with default arguments. So you could write: Event.observe(window, 'mouseup', alert.curry('hi')); Joe As long as you know what curry does, I think this is more obvious for really simple examples like this. On Jan 3, 10:50 am, "T.J. Crowder" wrote: > > Also any reference to a function inside of an observer needs to be in > > the form of an anonymous function, not a literal function itself. > > Doesn't have to be anonymous. Named functions are fine: > > function myMouseUpThingy() { > alert('Howdy'); > > } > > // later > > Event.observe(window, 'mouseup', myMouseUpThingy); > > The critical thing is that you need to pass in a function *reference*. > > -- T.J. > > On Jan 3, 4:45 pm, Walter Lee Davis wrote: > > > Try doing this instead: > > > //put the following in a script block near the bottom of your html > > var foo = function(){ alert('howdy') }; > > Event.observe(window,'mouseup',foo); > > > Note that the function is called Event.observe, not Event.observer. > > Also any reference to a function inside of an observer needs to be in > > the form of an anonymous function, not a literal function itself. You > > could have done this as well: > > > Event.observe(window,'mouseup',function(){alert('howdy')}); > > > It's a subtle distinction, but it becomes much more important in a non- > > trivial example, because an anonymous function won't get its input > > variables (and won't be called, either) until it is invoked. Then it > > "uncloaks", does its business, and vanishes again. > > > Walter > > > On Jan 2, 2009, at 1:30 PM, Per wrote: > > > > Hi, > > > I'm new to protocoljs, but it's this thing I'm trying to do, and it > > > doesn't work. It drives me mad. > > > > Anyway, > > > I try to do this with protocoljs: > > > > > > > This was how I wrote it, but it doesn't work: > > > Event.observer(window,'mouseup',alert('hi')); > > > > What is the right way to do it? > > > > Thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Event.observer with mouseup?
> Also any reference to a function inside of an observer needs to be in > the form of an anonymous function, not a literal function itself. Doesn't have to be anonymous. Named functions are fine: function myMouseUpThingy() { alert('Howdy'); } // later Event.observe(window, 'mouseup', myMouseUpThingy); The critical thing is that you need to pass in a function *reference*. -- T.J. On Jan 3, 4:45 pm, Walter Lee Davis wrote: > Try doing this instead: > > //put the following in a script block near the bottom of your html > var foo = function(){ alert('howdy') }; > Event.observe(window,'mouseup',foo); > > Note that the function is called Event.observe, not Event.observer. > Also any reference to a function inside of an observer needs to be in > the form of an anonymous function, not a literal function itself. You > could have done this as well: > > Event.observe(window,'mouseup',function(){alert('howdy')}); > > It's a subtle distinction, but it becomes much more important in a non- > trivial example, because an anonymous function won't get its input > variables (and won't be called, either) until it is invoked. Then it > "uncloaks", does its business, and vanishes again. > > Walter > > On Jan 2, 2009, at 1:30 PM, Per wrote: > > > > > Hi, > > I'm new to protocoljs, but it's this thing I'm trying to do, and it > > doesn't work. It drives me mad. > > > Anyway, > > I try to do this with protocoljs: > > > > > This was how I wrote it, but it doesn't work: > > Event.observer(window,'mouseup',alert('hi')); > > > What is the right way to do it? > > > Thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Event.observer with mouseup?
Try doing this instead: //put the following in a script block near the bottom of your html var foo = function(){ alert('howdy') }; Event.observe(window,'mouseup',foo); Note that the function is called Event.observe, not Event.observer. Also any reference to a function inside of an observer needs to be in the form of an anonymous function, not a literal function itself. You could have done this as well: Event.observe(window,'mouseup',function(){alert('howdy')}); It's a subtle distinction, but it becomes much more important in a non- trivial example, because an anonymous function won't get its input variables (and won't be called, either) until it is invoked. Then it "uncloaks", does its business, and vanishes again. Walter On Jan 2, 2009, at 1:30 PM, Per wrote: > > Hi, > I'm new to protocoljs, but it's this thing I'm trying to do, and it > doesn't work. It drives me mad. > > Anyway, > I try to do this with protocoljs: > > > This was how I wrote it, but it doesn't work: > Event.observer(window,'mouseup',alert('hi')); > > What is the right way to do it? > > Thank you! > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Event.observer with mouseup?
Hi Per, > This was how I wrote it, but it doesn't work: > Event.observer(window,'mouseup',alert('hi')); There are a couple of problems with that code. First, you're calling Event.observer (with an 'r' at the end); the method is Event.observe (no 'r'). The second problem is that you're calling the alert() method and then passing its result into the Event.observe call. You want to pass a reference to a function object to Event.observe instead. (You can do this in many ways. One common way is to wrap the code in an anonymous function declaration. For anything of any size, I recommend named functions instead.) There are lots of examples of event handlers, with discussion, on the unofficial Prototype & script.aculo.us wiki: http://proto-scripty.wikidot.com. Look under "Prototype Tips". HTH, -- T.J. Crowder tj / crowder software / com On Jan 2, 6:30 pm, Per wrote: > Hi, > I'm new to protocoljs, but it's this thing I'm trying to do, and it > doesn't work. It drives me mad. > > Anyway, > I try to do this with protocoljs: > > > This was how I wrote it, but it doesn't work: > Event.observer(window,'mouseup',alert('hi')); > > What is the right way to do it? > > Thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---