Anantha Padmanaban Anantha Padmanaban - 25 days ago 5
Javascript Question

Can't access self variable inside query function

//This is a Route
function doSomething(){
var self=this;
var a,b,c;
//do something
reqArray="somevalue";
this.store.query("record",{"filter":"[{'api_name':'somevalue','comparator':'somevalue','value':"+reqArray+"}]"})
.then(function(data){
//can't access a,b,c or self
});
}


I get the data i need inside the then function after the promise is resolved but i am not able to access anything outside the then function.

Editing for more clarification: this variable inside then function is window.i want to be able to reference the route .

Answer

If you really need all your vars in your object, you can do something like this :

function doSomething(){  
    var self=this;   
    var a,b,c;
    //do something
    // Save value
    self.a = a;
    self.b = b;
    self.c = c;
    reqArray="somevalue";   
    this.store.query("record",{"filter":"[{'api_name':'somevalue','comparator':'somevalue','value':"+reqArray+"}]"}).then(
        (function(data){                     
            // this = self
            // this.a = a
            // ...            
        }).bind(self));  
}
Comments