Created
December 3, 2010 15:09
-
-
Save jarrettmeyer/727075 to your computer and use it in GitHub Desktop.
jQuery deleteLink plugin
This file contains 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 ($) { | |
$.fn.deleteLink = function (options) { | |
var defaults = { | |
event: "click", // Type of event to bind. This will almost always be a "click" event. | |
url: undefined, // If the URL is undefined, then we will find the URL on the element. | |
attr: "href", // Attribute that contains the URL. If a link, then this is probably an "href" attribute. | |
data: "", // Data sent with the AJAX request. | |
dataType: "html", // Type of data returned on the AJAX request. | |
type: "DELETE", // Type of request to send. Probably should be either a "DELETE" or "POST". | |
confirm: "Deletes are permanent and cannot be undone. Are you ABSOLUTELY sure you wish to continue?", | |
success: function() { | |
window.location.reload(); | |
} | |
}; | |
var options = $.extend(defaults, options); | |
return this.each(function() { | |
$(this).bind(options.event, function(e) { | |
e.preventDefault(); | |
// If no URL has been set, then get the URL from the | |
// appropriate HTML element attribute. | |
if (!options.url) { | |
options.url = $(this).attr(options.attr); | |
console.log("URL: " + options.url); | |
} | |
if (options.confirm) { | |
var confirmResult = window.confirm(options.confirm); | |
if (confirmResult) { | |
sendDeleteRequest(); | |
} | |
} else { | |
// else, there are no confirm settings... | |
sendDeleteRequest(); | |
} | |
}); | |
}); | |
function sendDeleteRequest() { | |
console.log("Sending delete request to " + options.url); | |
$.ajax({ | |
url: options.url, | |
type: options.type, | |
data: options.data, | |
dataType : options.dataType, | |
success: options.success, | |
error: function() { | |
// A handy error message, just in case anything goes pear-shaped. | |
window.alert("An error occurred while sending the delete request."); | |
} | |
}); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment