0

I want to assign value to a class instance variable from within ajax success functions, code will explain better what I mean.

var SomeClass = function() {
    this.someMethod = function() {
        $.ajax({
            method: 'GET',
            url: 'http://example.com',
            success: function(resp) {
                var js = JSON.parse(resp);
                // I want to assign js object to SomeClass.response instance variable
            };
        });
    };
};

If I try this.response, it obviously doesn't work. If I assign this to some variable before I make ajax call, It doesn't work either. I mean this:

var SomeClass = function() {
    this.someMethod = function() {
        // Asign this to self
        var self = this;
        $.ajax({
            method: 'GET',
            url: 'http://example.com',
            success: function(resp) {
                var js = JSON.parse(resp);
                // I want to assign js object to SomeClass.response instance variable
                self.response = js;  // However it doesn't work
            };
        });
    };
};

I'll appreciate your help!!!

4
  • Can you show how you intend to use response later. Commented Oct 12, 2014 at 10:00
  • var someVariable = SomeClass(); someVariable.response; Commented Oct 12, 2014 at 10:01
  • Ok, this will be an instance of SomeClass. Then you probably do soveVariable.someMethod(). And what next? Commented Oct 12, 2014 at 10:02
  • someVariable.response Commented Oct 12, 2014 at 10:03

2 Answers 2

1

Since AJAX is asynchronous, you can't use someVariable.response until after the AJAX call completes. The appropriate way is to have someMethod take a callback:

var SomeClass = function() {
    this.someMethod = function(callback) {
        // Asign this to self
        var self = this;
        $.ajax({
            method: 'GET',
            url: 'http://example.com',
            success: function(resp) {
                var js = JSON.parse(resp);
                // I want to assign js object to SomeClass.response instance variable
                self.response = js;
                callback();
            };
        });
    };
};

Then you would use it like this:

var someVariable = new someClass;
someVariable.someMethod(function() {
    console.log(someVariable.response);
});
Sign up to request clarification or add additional context in comments.

Comments

1

While @Barmar solution will work, i think the best way its to just use promises.. and since you are using jQuery already this is pretty easy. See below:

var SomeClass = function() { this.someMethod = function() { return $.ajax({ method: 'GET', url: 'http://example.com' }); }; };

And then you do something like this:

var someVariable = new SomeClass();
    someVariable.someMethod().then(function(returnedValue){
        console.log(JSON.parse(returnedValue));
});

I believe that promises is the way to go and since they will be included in ES6... its better to familiarise yourself with the concept.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.