From doing some trial and error code I've determined that the first options is the best. Here is a simplified version of the code that seems to work best.
_create: function() {
var self = this;
self._doAjax(self.doStuff);
},
_doAjax: function(onSuccess) {
var self = this;
$.ajax({
context: self,
success: function (data) {
onSuccess.call(this, data);
self._trigger('dataLoaded', null, data);
}
});
},
doStuff: function(data) {
var self = this;
console.log(self);
console.log(data);
}
This allows me to use a callback like I normally would and allows the user of my widget to use the dataLoaded
event if it is needed. The $.ajax
content:
option coupled with onSuccess.call(this,data)
allows me to ensure this
will still refer to my widget from inside the doStuff
method that was used as the onSuccess
callback.