iterate over the togglers with each and then you'll have a pointer to the
toggler (and can bind this to your class).
this.togglers.each(function(tog) {
  tog.addEvent('click', function() {
    tog.getParent()....
  }.bind(this);
}, this)

In general, "this" should *always* point to the instance of your class.

On Fri, Jul 24, 2009 at 10:35 AM, Serkan T <[email protected]> wrote:

>
> hi,
>
> I have problems with binding and this references in a function. i.e
> using two different this reference in a function . Here is the case:
>
> var myNewClass  = new Class({
>    options: {
>      /*bla bla*/
>    },
>    initialize: function(){
>        this.togglers = $$('ul');
>        this.togglers.addEvent('click',function(){
>            var childContainer= this.parentNode.getElement('ul');
>            this.myAlert(childContainer);
>        })
>    },
>    myAlert: function(message){
>        alert(message)
>    }
> })
>
>
> Browser returns an error saying this.myAlert is undefined
>
> I know I can overcome this by using .bind(this) to addEvent function
> but in this case this.parentNode becomes undefined.
>
> in fact this references two different things for this.parentNode and
> this.myAlert
>
> I'm sure there is a simple solution for this but cant see it actually
>
> Thanks for your help
>

Reply via email to