0

I have an object x of class C. The attribute x.myval should be set by calling x.load(), which in turn should get its data from an asynchronous ajax call.

class C {
    constructor() {
        this.myval = "hello";
    }
    load() {
        return $.ajax({
            type: "GET",
            url: "file.txt",
            // suppress "xml not well-formed error in firefox", 
            beforeSend: function(xhr){
                if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
            },
            contentType: "text/plain",
            dataType: "text",
            success: function(text) {
                this.myval = text;
            }
        });
    }
}

var x = new C();
$.when(x.load()).done(function(a1,a2) {
    console.log(x.text); //should print the content of file.txt
});

I get an error this.myval is undefined, obviously because this is set to this of jquery-$.

I also tried:

class C {
    constructor() {
        this.myval = "hello";
    }
    load() {
        var callback = function callbackClosure(mythis) {return function(text) {
            this.myval = text;
        }}(this);

        return $.ajax({
            type: "GET",
            url: "file.txt",
            // suppress "xml not well-formed error in firefox", 
            beforeSend: function(xhr){
                if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
            },
            contentType: "text/plain",
            dataType: "text",
            success: callback
        });
    }
}

var x = new C();
$.when(x.load()).done(function(a1,a2) {
    console.log(x.text); //should print the content of file.txt
});

But this trew an exception jQuery.Deferred exception: assignment to undeclared variable...

1
  • Use an arrow function. Commented Aug 20, 2018 at 12:50

1 Answer 1

1

this doesn't refer to your instance in this line:

success: function(text) {
    this.myval = text;
}

You can make a temp variable referring to your instance in the outer method, like this:

load() {
    var that = this;
    return $.ajax({
        type: "GET",
        url: "file.txt",
        // suppress "xml not well-formed error in firefox", 
        beforeSend: function(xhr){
            if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
        },
        contentType: "text/plain",
        dataType: "text",
        success: function(text) {
            that.myval = text;
        }
    });
}

Or, you can use an arrow function:

load() {
    return $.ajax({
        type: "GET",
        url: "file.txt",
        // suppress "xml not well-formed error in firefox", 
        beforeSend: function(xhr){
            if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
        },
        contentType: "text/plain",
        dataType: "text",
        success: (text) => {
            this.myval = text;
        }
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

They don't have to... there are more elegant solutions. Also, in the future if there's a duplicate (canonical) could you also vote to close as well instead of answering right away? This is an incredibly common problem.
Yeah, I rephrased that. Arrow function is the way to go.
@Li357 it is indeed extremely common. I had answered the exact same thing about an hour before this one stackoverflow.com/questions/51929737. But it is hard to mark this as a duplicate of another question. I couldn't find any question that would fit.
Then don't keep answering. Vote to close as duplicates.
@Li357 think of noobs like me: I'm glad I got an answer. I have been searching for more than an hour before I posted this question.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.