/* | |
Exemples : | |
<a href="posts/2" data-method="delete" data-token="{{csrf_token()}}"> | |
- Or, request confirmation in the process - | |
<a href="posts/2" data-method="delete" data-token="{{csrf_token()}}" data-confirm="Are you sure?"> | |
*/ | |
(function() { | |
var laravel = { | |
initialize: function() { | |
this.methodLinks = $('a[data-method]'); | |
this.token = $('a[data-token]'); | |
this.registerEvents(); | |
}, | |
registerEvents: function() { | |
this.methodLinks.on('click', this.handleMethod); | |
}, | |
handleMethod: function(e) { | |
var link = $(this); | |
var httpMethod = link.data('method').toUpperCase(); | |
var form; | |
// If the data-method attribute is not PUT or DELETE, | |
// then we don't know what to do. Just ignore. | |
if ( $.inArray(httpMethod, ['PUT', 'DELETE']) === - 1 ) { | |
return; | |
} | |
// Allow user to optionally provide data-confirm="Are you sure?" | |
if ( link.data('confirm') ) { | |
if ( ! laravel.verifyConfirm(link) ) { | |
return false; | |
} | |
} | |
form = laravel.createForm(link); | |
form.submit(); | |
e.preventDefault(); | |
}, | |
verifyConfirm: function(link) { | |
return confirm(link.data('confirm')); | |
}, | |
createForm: function(link) { | |
var form = | |
$('<form>', { | |
'method': 'POST', | |
'action': link.attr('href') | |
}); | |
var token = | |
$('<input>', { | |
'type': 'hidden', | |
'name': '_token', | |
'value': link.data('token') | |
}); | |
var hiddenInput = | |
$('<input>', { | |
'name': '_method', | |
'type': 'hidden', | |
'value': link.data('method') | |
}); | |
return form.append(token, hiddenInput) | |
.appendTo('body'); | |
} | |
}; | |
laravel.initialize(); | |
})(); |
This comment has been minimized.
This comment has been minimized.
Great, thanks for the update :) |
This comment has been minimized.
This comment has been minimized.
Nice job ;) |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Hi all, i've change a little bit to support jQuery promise so you can use any confirm action like bootbox or swal https://gist.github.com/ghprod/0bb7f8d207ba7838a0e6 Thanks |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Got a problem with this. I get redirected to the |
This comment has been minimized.
This comment has been minimized.
It's actually triggering the |
This comment has been minimized.
This comment has been minimized.
thanks its work! |
This comment has been minimized.
This comment has been minimized.
Cool! |
This comment has been minimized.
This comment has been minimized.
Doesn't work for me, redirects to view method |
This comment has been minimized.
This comment has been minimized.
Same here, doesn't work for me redirecting to view. |
This comment has been minimized.
This comment has been minimized.
Great, thanks for the update :) |
This comment has been minimized.
This comment has been minimized.
no idea why, but doesn't work :( |
This comment has been minimized.
Hey, Thanks alot!
Going to use this in my interview test!
Again! Thanks for the update to 5.0^