This is my first attempt at a jQuery plugin and I have managed to cobble something together that is nearly working. I have a button that, once pressed, sends an ID via ajax in order to delete an image. My problem is that when the ajax call returns I am getting the following error in my Firebug console window:
TypeError: self.releaseCropBoxIfSet is not a function
Here is a stripped down version of my plugin:
if( typeof Object.create !== 'function' ){
Object.create = function( obj ){
function F() {};
F.prototype = obj;
return new F();
};
}
(function( $, window, document, undefined ) {
var uploadCrop = {
init: function (options, elem ){
var self = this;
self.elem = elem;
self.$elem = $( elem );
self.options = $.extend( {}, $.fn.uploadCropPlugin.options, options);
self.deleteUploadedImageButton = $('#'+self.options.deleteUploadedImageButton);
self.ajaxurl = self.options.ajaxurl;
self.deleteUploadedImageButton.on('click', function(e) {
$(this).hide();
$.ajax({
url: self.ajaxurl,
type: 'post',
success: self.deleteSuccess,
error: self.deleteError,
data: {
IDtoDelete : IDtoDelete,
action : 'deleteImage'
}
});
});
},
deleteSuccess: function(data) {
var self = this;
var returnedData = $.parseJSON(data);
self.releaseCropBoxIfSet(); // I am getting an error here
},
releaseCropBoxIfSet: function() {
var self = this;
// REMOVE OLD JCROP
if (typeof self.jcrop_api != "undefined") {
self.jcrop_api.release();
self.jcrop_api.destroy();
}
}
};
$.fn.uploadCropPlugin = function( options ) {
return this.each(function() {
var thisUploadCropObj = Object.create( uploadCrop );
thisUploadCropObj.init( options, this );
});
};
$.fn.uploadCropPlugin.options = {
// my vars here
};
})( jQuery, window, document );
I wondered whether the issue was that self gets lost during the Ajax call. So I tried sending self as a variable to the function and then returning it but I got errors that way. I also tried leaving the self. bit off the front of the function call but it still doesn't work.