Firstly, I'm new to Javascript, so this probably just my misunderstanding so apologies.
I have found that when debugging my Javascript, the value of this
changes within my prototype methods and causes irregularities else where within the other prototype methods.
It (this
) starts off with reference to the object for which the method belongs, and then later on it changes to Window
and when i try to reference method this.request
in the method Clarizen.prototype.getSessionId
the browser gives me the following error:
Uncaught TypeError: this.request is not a function
I checked using Chrome's debugger and the value of this
changes to Window when it enters this method.
Here is the code:
var Clarizen = function (userName, password, applicationId, optionalSessionId) {
if (userName === undefined) {
throw new Error('userName is missing');
}
if (password === undefined) {
throw new Error('password is missing');
}
this.userName = userName;
this.password = password;
this.applicationId = applicationId;
this.sessionId = (optionalSessionId === undefined) ? '' : optionalSessionId;
this.constants = ClarizenConstants.Urls;
this.jsonUser = null;
};
Clarizen.prototype.convertToObject = function(json)
{
console.log("convertToObject: " + json);
return JSON.parse(json);
};
Clarizen.prototype.debug = function (message) {
console.log("clarizen object debug " + message);
};
Clarizen.prototype.validateMethod = function (method) {
if (method === "GET" || method === "POST" || method === "HEAD") {
return true;
}
return false;
};
Clarizen.prototype.request = function (options) {
var url = (options.url !== null) ? options.url : null;
var callback = (typeof options.callback === "function") ? options.callback : null;
var method = (options.method !== null) ? options.method : "GET";
if (url === null)
{ throw new Error("url missing"); }
if (callback === null)
{ throw new Error("callback is missing/not a function type"); }
if (!Clarizen.prototype.validateMethod(options.method)) {
throw new Error("method can isnt supported currently " + method)
}
var util = new Bradaz();
var xhr = util.createXHR(url, method, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = callback(xhr.responseText);
return response;
}
};
if (options.body !== null) {
xhr.send(options.body)
}
else {
xhr.send();
}
};
Clarizen.prototype.getSessionId = function(response)
{
var loginResponse = JSON.parse(response);
console.log("getSessionId " + loginResponse.serverLocation);
//The followin ERRORS
var r = this.request (
{
url: this.serverLocation + ClarizenConstants.Urls.authentication,
method: "POST",
callback: this.convertToObject,
body: this.jsonUser
}
);
};
Clarizen.prototype.login = function () {
var obj = new ClarizenObject();
var user = obj.factory({
type: 'User',
userName: "username",
password: "password"
});
var jsonBody = JSON.stringify(user);
this.jsonUser = jsonBody;
var response = this.request (
{
url: ClarizenConstants.Urls.getServerDefinition,
method: "POST",
callback: this.getSessionId,
body: jsonBody
}
);
};
We enter the object through the login
prototype method (in pastebin):
var clarizen = new Clarizen(user.userName, user.password);
clarizen.login();