Skip to content

Instantly share code, notes, and snippets.

@rauhryan
Forked from mikeobrien/ajax.js
Created October 22, 2011 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rauhryan/1305317 to your computer and use it in GitHub Desktop.
Save rauhryan/1305317 to your computer and use it in GitHub Desktop.
Boiler plate code to delete a record + row
(function($){
$.addAjaxItemDeleteHandler = function(options) {
var defaults = {
url: 'delete',
data: function(id) { return { Id: id }; },
deleteElementClass: 'item-delete',
deleteElementIdAttribute: 'data-id',
deleteElementDescriptionAttribute: 'data-description',
deleteRowClass: 'item-row',
deleteRowIdAttribute: 'data-id',
confirmHandler: function(id, description) { return confirm('Are you sure you want to delete ' + description + '?'); },
actionSuccess: function(data) { return true; },
actionErrorMessage: function(data) { return 'An unknow error has occured.'; },
responseHandler: function(id, description, data) { alert('Successfully deleted ' + description + '.'); },
errorHandler: function(id, description, message) { alert('An error has occured deleting ' + description + ': ' + message); },
communicationErrorMessage: 'We are unable to talk to the server at this time.'
};
options = $.extend(defaults, options);
this('.' + options.deleteElementClass).click(function() {
var id = $(this).attr(options.deleteElementIdAttribute);
var description = $(this).attr(options.deleteElementDescriptionAttribute);
if (options.confirmHandler(id, description)) {
$.post(options.url, options.data(id), function(data) {
if (options.actionSuccess(data))
$("." + options.deleteRowClass + "[" + options.deleteRowIdAttribute + "='" + id + "']").remove();
else options.errorHandler(id, description, options.actionErrorMessage(data));
}).error(function() { options.errorHandler(id, description, options.communicationErrorMessage) });
}
return false;
});
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment