I forgot how flexible javascript is...

Managed to solve my problem by adding a property to the element
object.

see here for solution and demo

http://mootools.net/shell/XP7ZU/2/

Hopefully someone else finds it usefuL :)



On Apr 1, 9:04 pm, Ryan Florence <[email protected]> wrote:
> Put a test case onhttp://motools.net/shellwith comments about what you do and 
> don't want to happen.
>
> ---
> Ryan Florencehttp://ryanflorence.com
>
> On Apr 1, 2010, at 6:52 PM, keeger wrote:> Hello.
>
> > I've created a page wrapper class to contain my javascript, partly to
> > avoid thrashing with global vars etc, and just organizationally i like
> > it.
>
> > I'm having trouble.
>
> > I've created the class following the standard mootools class;
>
> > var pageClass = new Class( {
> >     Implements: [Events],
> >     globalControls:  function() {
> >               this.account = $(document.body).getElement("[id
> > $=txtAccount]");
> >               this.randomVariable = true;
> >     },
> >     gControls: null,
> >     initialize: function() {
> >          this.gControls = new globalControls();
> >          this.gControls.account.addEvent('blur', function() {
> >                    //this == the account element at this point.
> >                   alert(this.value);  //works
> >                   alert(this.gControls.randomVariable);  //fails but
> > is desired.
> >        });
> > });
>
> > Now, I've tried putting "this" after the function() {} in addEvent, to
> > re-direct this to the object:  like so:
>
> > ...//SNIPPED
>
> >      initialize: function() {
> >          this.gControls = new globalControls();
> >          this.gControls.account.addEvent('blur', function() {
> >                    //this == the account element at this point.
> >                   alert(this.value);  //works
> >                   alert(this.gControls.randomVariable);  //fails
> >                   alert(this.gControls.account.value);  //would be
> > acceptable if this were re-set to be the pageClass instance.
> >        }, this);   //still fails.  this never points to anything but
> > the account input
>
> > globalControls is an inner class to hold references to DOM elements.
> > I do this, since I'm using .Net, and I have to search by [id
> > $=elementName], and I don't want to slow things down by doing this
> > everytime I use a control.
>
> > Interestingly enough, the very first time through (initialize),
> > this.gControls is a recognized variable.  It's on the subsequent calls
> > that it is not (when an actual blur happens).
>
> > So my question is this:  Is there a way inside a class to make an
> > event function aware of the class?  If so, how?  I am trying to avoid
> > having to use actual global variables.   The pages involved use a div
> > that's populated via ajax and another page, and while I could code
> > globals that are intersect free per se, I would rather a neater, more
> > encapsulated approach.
>
> > Thanks for any help
>
> > --
> > To unsubscribe, reply using "remove me" as the subject.

Reply via email to