I want to fire off an AJAX call when a plugin is created that will retrieve some data and then continue building the plugin control. So given this simplified version:
$.fn.grid = function (options) {
this.each(function () {
var $this = $(this); // Somehow to maintain $this for the callback
buildPluginBasics($this);
$.getJSON("DataPath", { option1:"etc" }, dataReceived);
});
function dataReceived(data, status) {
buildGridRows($theCallingInstance, data);
}
}
You can see I’ve not got any way of knowing which plugin element I now need to continue building. I basically need a reference to the original $this to be available in dataReceived. Can anyone point me in the right direction?
You can use the full
$.ajax()call with thecontextoption, like this:And in your method,
thiswill be the context above, so$thisfrom the original.Alternatively, use an anonymous method and pass an additional argument, e.g.:
And add it as a parameter on your method: