Hello,
I have been lurking as a guest of this forum for a long time and have had many questions answered, thank you all!
Now it is my turn to pose a question (which I have searched for answers on here, there and everywhere – maybe it’s not knowing exactly what to search for eh?):
I am rewriting a site that began as a hobby (it still is) but got a tad spaghetti like, for this I am trying to make a nice tight JS object model.
I have created a user object that has properties: userName and isLoggedIn along with login and logout methods amongst others. Ideally I like like to make use of AJAX like so:
[CODE]
function user()
{
//Properties
this.isLoggedIn=false;
this.userName=””;
//Methods
this.login = function(strUserName,strPassword)
{
requestData(“ajax/login.php”,”user_name=”+strUserName+”&password=”+strPassword,loginContinue);
}
function loginContinue()
{
if (request.readyState == 4)
{
var xmlDoc = request.responseXML;
var elmLoggedInResult = xmlDoc.documentElement.getElementsByTagName(“response”);
var strLoginResult=elmLoggedInResult[0].getAttribute(“result”);
this.userName=elmLoggedInResult[0].getAttribute(“username”);
if(strLoginResult == “Logged in”)
{
this.isLoggedIn=true;
}
else
{
alert(strLoginResult);
}
}
}
}
What happens with the above code is that the use of “this” in loginContinue has been disconnected from the current object (at least in Firefox – not checked in IE) so that the properties do not get updated – although no errors occur (GRR). I have got it to work by explicetly setting the properties of the declared object but that is not pretty or transferable i.e.:
[code]
var theUser = new user();
//In loginContinue:
theUser.isLoggedIn=true;
Is there a nicer way of doing this?