Hello all, Given the following code: var clicklink = Class.create( { onmousedown : function(e){ alert(''here''); Event.stop(e); }, initialize : function() { $$("span").each(function(item, index) { Event.observe(item, ''mousedown'', this.onmousedown, false); }); } }); var clink = new clicklink(); I''m getting the following error when a span is clicked: handler has no properties file:prototype.js Line 3842 Prototype version = ''1.6.0.2'' What am I not understanding? Thanks for the help. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 30, 2008 1:46 PM, wte <wyatt.eurich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What am I not understanding?In anonymous functions like that (you''re iterator), you need to bind to the current instance of your object. So this:> initialize : function() { > $$("span").each(function(item, index) { > Event.observe(item, ''mousedown'', this.onmousedown, false); > });Would become:> initialize : function() { > $$("span").each(function(item, index) { > Event.observe(item, ''mousedown'', this.onmousedown.bindAsEventListener(this), false); > }.bind(this));-justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thank-you so much, I had tried the bindAsEventListener before and just got different errors. I did not catch that I needed to bind the iterator. Works great now. On Jan 30, 3:08 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 30, 2008 1:46 PM, wte <wyatt.eur...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > What am I not understanding? > > In anonymous functions like that (you''re iterator), you need to bind > to the current instance of your object. So this: > > > initialize : function() { > > $$("span").each(function(item, index) { > > Event.observe(item, ''mousedown'', this.onmousedown, false); > > }); > > Would become: > > > initialize : function() { > > $$("span").each(function(item, index) { > > Event.observe(item, ''mousedown'', this.onmousedown.bindAsEventListener(this), false); > > }.bind(this)); > > -justin--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 30, 2008 2:29 PM, wte <wyatt.eurich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I did not catch that I needed to bind the iterator.Yes, you are binding the iterator so that you can use ''this'' and have the same context inside the anonymous function as you have outside the function. Think of it like a means to pass the ''this'' object into a sub-function. -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---