Wednesday, February 25, 2009

[jQuery] Custom Callback not using jQuery object

I have built a custom callback into my plugin, here is an example:

$.fn.matrixStatus = function(options) {
var defaults = {
urlSuffix: '?action=live',
onComplete: function() {}
};

var options = $.extend(defaults, options);

return this.each(function() {
var obj = $(this);
var itemDesc = obj.attr('rel');
var itemId = obj.attr('id');
var itemHref = obj.attr('href');
obj.click(function() {
if (!itemDesc == '') {
var question = confirm('Are you sure you want to change the status
of "'+itemDesc+'"?');
} else {
var question = confirm('Are you sure you want to change the
status?');
}
if (question) {
$.ajax({
type: 'POST',
url: itemHref + defaults.urlSuffix
});

// Run our custom callback
defaults.onComplete();

}
return false;

});

});

};

For some reason when I try to use that function for a custom callback,
it won't allow me to get the jQuery object that the plugin is
targeting, so using $(this) within the onComplete function doesn't
work and give me errors. Any idea why this would be?

No comments: