As much as I love to use anonymous functions try using a prebound
function reference.  Also by extracting the code to send the XHR from
the objects constructor you will allow more flexibility in the future.


var test = Class.create({

      initialize : function(url, val){
           this.val = val;
           this.responseHandle = this.handleResponse.bind(this);
           this.sendRequest();
      },
      sendRequest : function(){
           new Ajax.Request(url, {
               onSuccess : this.responseHandle,
               //other parameters below...
            }
      },
      handleResponse : function(xhr){
           alert(this.val + "\n" + xhr.responseText);
      }
});

On Sep 8, 6:57 am, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> It's a binding problem.  Event handlers just call functions, not
> methods (JavaScript doesn't *have* methods).  When the event handler
> calls the function, it doesn't define the "this" you're expecting.
> Details 
> here:http://blog.niftysnippets.org/2008/04/you-must-remember-this.htmlhttp://www.alistapart.com/articles/getoutbindingsituationshttp://blog.niftysnippets.org/2008/03/mythical-methods.html
>
> Use bind() to fix it [or 
> bindAsEventListener()]:http://www.prototypejs.org/api/function/bindhttp://www.prototypejs.org/api/function/bindAsEventListener
>
> HTH,
> --
> T.J. Crowder
> tj / crowder software / com
>
> On Sep 7, 10:34 pm, Yosh <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > I'm a problem with this code.
>
> > ////////////////////////////////////////////////////////////////////
> > var test = Class.create({
> >         val:null,
> >         initialize:function(val) {
> >                 //obligatoire pour fonctionner
> >                 this.val = val;
> >                 new Ajax.Request("file.html", {
> >                         method: 'POST',
> >                         evalScripts: true,
> >                         onSuccess: function(result) {
> >                                 alert("1 "+this.val); // => return 
> > 'undefined' => doesn't work?
> > why?
> >                                 alert("2 "+val); // => 'test', reference of
> > initialize:function(val) => ok
> >                                 this.func2(); // => doesn't work? why?
> >                         }
> >                 });
> >         }
> >         func2:function() {
> >                 alert("1 "+this.val); // => return 'test' => ok
> >         }});
>
> > new test("test");
> > ////////////////////////////////////////////////////////////////////
>
> > So, i don't understand why i can't get the value of "val" all the time
> > and in all function of the class.
>
> > Thank you for your help.
>
> > Yohann POLI
>
>
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to