/    Sign up×
Community /Pin to ProfileBookmark

Objectifying AJAX

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);
}
}
}
}
[/CODE]

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;
[/code]

Is there a nicer way of doing this?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 30.2006 — 
I have created a user object
[/quote]

Have u tried JSON?

var user={

isLoggedIn:false,

userName:''

}

............

user.userName=elmLoggedInResult[0].getAttribute("username");

if(strLoginResult == "Logged in")
{
user.isLoggedIn=true;
}
else
{
alert(strLoginResult);
}

.....
[/quote]
Copy linkTweet thisAlerts:
@yaaaayauthorJul 31.2006 — Thanks,

I've made a nice workaround where I save the "this" into a temporary global variable so that it gets picked up by the continue function. It's a nice in between option.
Copy linkTweet thisAlerts:
@KorJul 31.2006 — Take care when using [B]this[/B]. Whithin a closure it might slow the the browser/computer by leaking memory because of an endless recoursively balance between references (this occures mainly in IE).

See:

http://www.bazon.net/mishoo/articles.epl?art_id=824

I guess it is not your case, but, even so...
×

Success!

Help @yaaaay spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...