OO programming issue in javascript and ASP.NET AJAX

I'm trying to keep as much OO as possible, but ASP.NET AJAX seems to do something weird after returning from the server ...

function Person( personId ) {
var id = personId;
var firstName;
var lastName;

this.initializeStep1 = function() {
    PeopleServices.getFirstName(id, this.initializeStep2);
}

this.initializeStep2 = function(foundFirstName) {
    alert(foundFirstName);
    firstName = foundFirstName;
    PeopleServices.getLastName(id, this.initializeStep3);
}

this.initializeStep3 = function(foundLastName) {
    alert(foundLastName);
    alert(firstName);
    lastName= foundLastName;
} 

this.initializeStep1();

      

}

This is the foundation. So it's basically creating a person and getting their first and last name from the server to initialize the person.

When I create a new person, it goes through initializeStep1, calls the server's getFirstName webmethod, and eventually reaches initializeStep2. warning (foundFirstName); works, it notifies the name that was found and that's right ... now after setting the personal variable firstName to what was found, I make a second call to the server ...

It won't reach initializeStep3 this time and I know it shouldn't have crashed on the server because even if I replace the line

PeopleServices.getLastName(id, this.initializeStep3);

      

from

PeopleServices.getFirstName(id, this.initializeStep3);

      

it still doesn't work.

I was wondering if after the first call to the server it lost its reference to "this" or something similar to what happened where I cannot call initializeStep3 the way I do. Does anyone have any ideas?

some notes:

  • I know all the server web methods work, I tested them separately.
  • I'm not sure if my OO is here.
  • I'm not sure if I'm AJAX right too.

Thanks for your help!

0


a source to share


1 answer


You need to close



var that = this;
PeopleServices.getFirstName(id, function (x) { 
                                    that.initializeStep3(x);
                                } 
);

      

+3


a source







All Articles