Skip to content

Instantly share code, notes, and snippets.

@dirkpostma
Last active July 4, 2018 12:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dirkpostma/5442850 to your computer and use it in GitHub Desktop.
Save dirkpostma/5442850 to your computer and use it in GitHub Desktop.
Restful DELETE request using <a>
$(document).ready(function() {
// Add click handler to hyperlinks to send restful DELETE requests
//
// Example:
//
// <a href="/delete/1" class="rest-delete">delete</a>
// <script>restful.init($('.rest-delete'));</script>
//
var restful = {
// TODO: add various configurations, e.g.
// - do_confirm: [ true | false ]
// - confirm_message: "Are you sure?"
// - do_remove_parent: [ true | false ]
// - parent_selector: '.li' '.div' ...
// - success: (closure)
init: function(elem) {
elem.on('click', function(e) {
self=$(this);
e.preventDefault();
if(confirm('Are you sure?')) {
$.ajax({
url: self.attr('href'),
method: 'DELETE',
success: function(data) {
self.parent('li').remove(); // todo: make configurable
},
error: function(data) {
alert("Error while deleting.");
console.log(data);
}
});
};
})
},
}
restful.init($('.rest-delete'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment