Solution for using `this` keyword in ajax calls within methods?
- by dqhendricks
I am creating a JavaScript class. Some of the methods contain AJAX calls using JQuery. The problem I am comming accross is that I cannot use the this keyword within the AJAX callbacks due to a change in scope. I have come up with a hackey solution, but I am wondering what is the best practice way around this?
Here is an example:
var someClass = function() {
var someElement = $('form');
this.close = function() {
someElement.remove();
};
this.query = function() {
$.ajax({
url: someurl,
success: function() {
this.close(); // does not work because `this` is no longer the parent class
}
});
};
};