Created
October 21, 2011 02:48
-
-
Save mikeobrien/1302991 to your computer and use it in GitHub Desktop.
Boiler plate code to delete a record + row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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