2

I don't realy understand why my variable is undefined This is my code:

Calendar = function() {

    this.data;  
    this.init = function(path, callback){
        $.ajax({
            url:path,
            type:'GET',
            success:function(data){
                this.data = data;
                console.log(this.data);
                callback();
            }
        })
    }

    this.create = function(){
        this.generateYear();
    }   

    this.generateYear = function(){
        console.log(this.data); 
    }   
}

And I use it like this:

$(document).ready(function(){
    var calendar = new Calendar();
    calendar.init(path,function(){
        calendar.create();
    });
});

So the first console.log is good but the second is undefined, I don't understand why because he is called after.

Thanks for your help

5
  • possible duplicate of How to return the response from an AJAX call? Commented Jul 26, 2013 at 8:25
  • @elclanrs Doesn't look like this one, there Commented Jul 26, 2013 at 8:25
  • what is callback(); after first console.log(this.data); Commented Jul 26, 2013 at 8:25
  • Hi use var k=this.data;in success function k=data; in generateYear = k; Commented Jul 26, 2013 at 8:27
  • You're right, my bad, it looked like it... Commented Jul 26, 2013 at 8:29

3 Answers 3

5

Set context param in ajax function. Try this one:

 $.ajax({
        url:path,
        type:'GET',
        context: this,
        success:function(data){
            this.data = data;
            console.log(this.data);
            callback();
        }
    })
Sign up to request clarification or add additional context in comments.

1 Comment

@MichaelWalter context. And it looks like a good answer. +1 from me.
3

this, in the callback you give to ajax, isn't your calendar.

Change your init function to

this.init = function(path, callback){
    var calendar = this;
    $.ajax({
        url:path,
        type:'GET',
        success:function(data){
            calendar.data = data;
            console.log(calendar.data);
            callback();
        }
    })
}

3 Comments

Thanks this is a good answer but I prefer Michael's answer, I find this more proper
@ant Personally I really prefer to always keep in mind the native closure based solution than to try to remember all the parameters of the ajax function. But his answer is good too (I upvoted it) so I won't reproach you to choose it. Simply be sure to understand my answer because it's the solution you'll use in all other similar cases :)
@ant dystroy answer is more readable .. Michael's answer using this will make confusion
0

Within this.generateYear your call to "this" is not more referencing your Calendar object and is referencing the Window object which most probably doesn't have data as an attribute. Make sure you read more about Javascript binding

Storing an instance of this in a variable within your object and later using it, would work. Example:

this.init = function(path, callback){
    var self = this;
    $.ajax({
        url:path,
        type:'GET',
        success:function(data){
            self.data = data;
            console.log(self.data);
            callback();
        }
    })
}

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.