Created
September 25, 2014 14:55
-
-
Save micc83/4a33c550f37972f84221 to your computer and use it in GitHub Desktop.
Ruby like delete links on laravel
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
/** | |
* Restfulizer | |
* | |
* Restfulize any hiperlink that contains a data-method attribute by | |
* creating a mini form with the specified method and adding a trigger | |
* within the link. | |
* Requires jQuery! | |
* | |
* Ex: | |
* <a href="post/1" data-method="delete">destroy</a> | |
* // Will trigger the route Route::delete('post/(:id)') | |
* | |
*/ | |
$(function () { | |
var makeRequest = function (target) { | |
var html = '<form action="'+target+'" method="POST">'; | |
html += '<input type="hidden" name="_method" value="DELETE">'; | |
html += '</form>'; | |
$(html).appendTo('body').submit(); | |
}; | |
$('a').on('click', function (e) { | |
var a = $(this); | |
if(a.data('method') === 'delete') { | |
e.preventDefault(); | |
confirm("Are you sure?") && makeRequest(a.attr('href')); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment