1

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();
cweiske
  • 30,033
  • 14
  • 133
  • 194
garfbradaz
  • 3,424
  • 7
  • 43
  • 70
  • 2
    Regarding the dupe, one place I saw that will lose the context of `this` is here: `callback: this.getSessionId,` - when the callback is run, `this` will be wrong within `getSessionId`. It can be fixed with `callback: this.getSessionId.bind(this)`. – James Thorpe Feb 10 '16 at 17:07
  • 1
    James you beautiful man - thanks for the speedy reply I will test tonight – garfbradaz Feb 10 '16 at 17:09
  • See the section *"Common problem: Using object methods as callbacks / event handlers"* in the duplicate. – Felix Kling Feb 10 '16 at 17:09
  • I should really get better at finding things on SO first it seems! Cheers guys – garfbradaz Feb 10 '16 at 17:11
  • Sometimes it's hard to find something if you don't exactly know what to look for :) No worries! – Felix Kling Feb 10 '16 at 17:22

0 Answers0