Last active
July 4, 2018 12:06
-
-
Save dirkpostma/5442850 to your computer and use it in GitHub Desktop.
Restful DELETE request using <a>
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
$(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