Last active
August 29, 2015 14:20
-
-
Save parweb/d933efbf0d7334a79ef9 to your computer and use it in GitHub Desktop.
Tag <a> with any method type for laravel
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
/* | |
* Utilisation: | |
* | |
* add this in your body tag | |
* <body data-token="{{ csrf_token() }}"> | |
* | |
* and enjoy ( that should change the world ^^ ) | |
* | |
* <a method="delete" href="{{ route( 'users.destroy', $user_id ) }}">Delete a user</a> | |
* <a method="put" data-items='{ "title" : {{ $mynewtitle }} }' href="{{ route( 'post.update', $post_id ) }}">change the title</a> | |
* | |
* PUT, POST, GET, PATCH, HEAD supported, even plop works ! | |
*/ | |
$(document).ready( function () { | |
$('body').on( 'click', 'a[method]', function ( e ) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
var $this = $(this); | |
var items = $(this).data('items') instanceof Object ? $(this).data('items') : {}; | |
var $form = $('<form/>', { | |
id : 'fake_form', | |
method : 'post'.toUpperCase(), | |
action : $this.prop('href') | |
}).appendTo('body'); | |
$method = $('<input/>', { | |
'type' : 'hidden', | |
'name' : '_method', | |
'value' : $this.attr('method'), | |
}).appendTo( $form ); | |
$token = $('<input/>', { | |
'type' : 'hidden', | |
'name' : '_token', | |
'value' : $('body').data('token'), | |
}).appendTo( $form ); | |
for ( var name in items ) { | |
var x = $('<input/>', { | |
'type' : 'hidden', | |
'name' : name, | |
'value' : items[name], | |
}).appendTo( $form ); | |
} | |
$form.submit(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test giscus