As noted in this blog post you can set the scope of this
in an anonymous function in Javascript.
Is there a more elegant way of scoping this
in the anonymous function call on success
of the AJAX request (i.e. not using that
)?
For example:
var Foo = {
bar: function(id) {
var that = this;
$.ajax({
url: "www.somedomain.com/ajax_handler",
success: function(data) {
that._updateDiv(id, data);
}
});
},
_updateDiv: function(id, data) {
$(id).innerHTML = data;
}
};
var foo = new Foo;
foo.bar('mydiv');
Using call but still have to name the parent object scope that
.
success: function(data) {
(function() {
this._updateDiv(id, data);
}).call(that);
}