I got stuck while coding and searching on for the past few hours hasn't helped me understand the following issue.
I have the following code:
var HomeController = function(){
this.hello = "Welcome on the homescreen";
this.user = null;
};
HomeController.prototype.setInterface = function(){
var self = this;
var api = new API();
// Set user info
var data = api.getUserInfo();
data.done(function(userdata){
$("#player").text("Welcome back, " + userdata.name + "!");
self.user = userdata;
});
console.log(this.hello);
console.log(this.user);
};
and I would like to make the user object as an property of the homecontroller.
I read about the use of the self variable, because context within the function would change but it does not seem to work here and I'm baffled.
The object contains data, it's not empty inside the function.
If it try to console log the following code, I get the following results:
Welcome on the homescreen
null
What am I doing wrong?