hmmm, I seem to need some help with scoping within functions for my classes. var clickShowTable=Class.create(); clickShowTable.prototype = { selectClassName:"selected", initialize:function(divID){ // }. clickActions:function(){ this.tdAs.each( function(a){ Event.observe(a, "click",function(event){ event.element().up("tr").addClassName(this.selectClassName); // **this.selectClassName not scoping }); }); }, What is the method to acheive this? Thanks, David --~--~---------~--~----~------------~-------~--~----~ 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 2/17/08, smartcookie <dpell.home-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > hmmm, I seem to need some help with scoping within functions for my > classes. > ...To bind "this" (or any other object) inside of a function, use Function#bind. Within Enumerable functions, you can simply pass "this" as the second argument instead. ... this.tdAs.each(function(a) {...}, this); // Prototype 1.6+ this.tdAs.each(function(a) {...}.bind(this)); // Prototype 1.5 ... - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David, Anonymous function that you use as event handler is implicitly bounded to a target element (1.6). You probably want to bind it to your class instance. ... Event.observe(a, ''click'', function(event) { event.element().up(''tr'').addClassName(this.selectClassName); }.bind(this)) ... Moreover, iterator function that you pass into "each" needs to be bounded to class instance as well (which is what Ken pointed out) Besides that, it might make sense to "cache" event handler (if performance is an issue): ... initialize: function() { ... this.onClick = this._onClick.bind(this); }, _onClick: function(e) { e.element().up(''tr'').addClassName(this.selectClassName); }, clickActions: function() { this.tdAs.invoke(''observe'', this.onClick) } ... Best, kangax On Feb 17, 6:54 am, smartcookie <dpell.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hmmm, I seem to need some help with scoping within functions for my > classes. > > var clickShowTable=Class.create(); > clickShowTable.prototype = { > selectClassName:"selected", > initialize:function(divID){ > //}. > > clickActions:function(){ > this.tdAs.each( > function(a){ > Event.observe(a, "click",function(event){ > event.element().up("tr").addClassName(this.selectClassName); // > **this.selectClassName not scoping > }); > }); > }, > > What is the method to acheive this? > > Thanks, > David--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Looking at your code, I''m assuming you''re using this code for selecting cells in a table... I that is the case, I strongly advise you to NOT attach eventlisteners to each <td>. It slows down your code considerably. Instead try attachng the event to the <table> or even better the <tbody> element. ... initialize: function(table) $(table).down(''tbody'').observe(''click'', function(ev) { this.clickListener(ev); }.bind(this)); }, clickListener: function(ev) { var tr = ev.findElement(''tr''); if (tr) { tr.addClassName(this.selectedClassName); } } ... Haven''t tested this piece of code, but I''m pretty sure it should work. What this does is observing click events on the tbody, and determining the clicked element then. If you have more than 100 rows things tend to get pretty laggy... Greetz, Wizz On Feb 17, 8:16 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> David, > > Anonymous function that you use as event handler is implicitly bounded > to a target element (1.6). You probably want to bind it to your class > instance. > ... > Event.observe(a, ''click'', function(event) { > event.element().up(''tr'').addClassName(this.selectClassName);}.bind(this)) > > ... > > Moreover, iterator function that you pass into "each" needs to be > bounded to class instance as well (which is what Ken pointed out) > Besides that, it might make sense to "cache" event handler (if > performance is an issue): > > ... > initialize: function() { > ... > this.onClick = this._onClick.bind(this); > > }, > > _onClick: function(e) { > e.element().up(''tr'').addClassName(this.selectClassName); > > }, > > clickActions: function() { > this.tdAs.invoke(''observe'', this.onClick)} > > ... > > Best, > kangax > > On Feb 17, 6:54 am, smartcookie <dpell.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > hmmm, I seem to need some help with scoping within functions for my > > classes. > > > var clickShowTable=Class.create(); > > clickShowTable.prototype = { > > selectClassName:"selected", > > initialize:function(divID){ > > //}. > > > clickActions:function(){ > > this.tdAs.each( > > function(a){ > > Event.observe(a, "click",function(event){ > > event.element().up("tr").addClassName(this.selectClassName); // > > **this.selectClassName not scoping > > }); > > }); > > }, > > > What is the method to acheive this? > > > Thanks, > > David--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks y''all. You guys rock. On Feb 18, 10:49 am, Wizz <woutaw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Looking at your code, I''m assuming you''re using this code for > selecting cells in a table... I that is the case, I strongly advise > you to NOT attach eventlisteners to each <td>. > It slows down your code considerably. Instead try attachng the event > to the <table> or even better the <tbody> element. > ... > initialize: function(table) > $(table).down(''tbody'').observe(''click'', function(ev) { > this.clickListener(ev); > }.bind(this)); > > }, > > clickListener: function(ev) { > var tr = ev.findElement(''tr''); > if (tr) { > tr.addClassName(this.selectedClassName); > }} > > ... > > Haven''t tested this piece of code, but I''m pretty sure it should work. > > What this does is observing click events on the tbody, and determining > the clicked element then. If you have more than 100 rows things tend > to get pretty laggy... > > Greetz, > > Wizz > On Feb 17, 8:16 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > David, > > > Anonymous function that you use as event handler is implicitly bounded > > to a target element (1.6). You probably want to bind it to your class > > instance. > > ... > > Event.observe(a, ''click'', function(event) { > > event.element().up(''tr'').addClassName(this.selectClassName);}.bind(this)) > > > ... > > > Moreover, iterator function that you pass into "each" needs to be > > bounded to class instance as well (which is what Ken pointed out) > > Besides that, it might make sense to "cache" event handler (if > > performance is an issue): > > > ... > > initialize: function() { > > ... > > this.onClick = this._onClick.bind(this); > > > }, > > > _onClick: function(e) { > > e.element().up(''tr'').addClassName(this.selectClassName); > > > }, > > > clickActions: function() { > > this.tdAs.invoke(''observe'', this.onClick)} > > > ... > > > Best, > > kangax > > > On Feb 17, 6:54 am, smartcookie <dpell.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > hmmm, I seem to need some help with scoping within functions for my > > > classes. > > > > var clickShowTable=Class.create(); > > > clickShowTable.prototype = { > > > selectClassName:"selected", > > > initialize:function(divID){ > > > //}. > > > > clickActions:function(){ > > > this.tdAs.each( > > > function(a){ > > > Event.observe(a, "click",function(event){ > > > event.element().up("tr").addClassName(this.selectClassName); // > > > **this.selectClassName not scoping > > > }); > > > }); > > > }, > > > > What is the method to acheive this? > > > > Thanks, > > > David--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---