Skip to content

Instantly share code, notes, and snippets.

@Fuitad
Forked from fer-ri/laravel.js
Last active May 28, 2016 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fuitad/0041f3f01f77fc9aa6aca05e92e2cee7 to your computer and use it in GitHub Desktop.
Save Fuitad/0041f3f01f77fc9aa6aca05e92e2cee7 to your computer and use it in GitHub Desktop.
You use Laravel 5 and you want to send a DELETE request without creating a form? This will handle the form-creation bits for you dynamically, similar to the Rails implementation. To use, import script, and create a link with the `data-method="DELETE"` attribute. Make sure you also add the proper <meta> tags in the <head> section of your HTML pag…
/*
* Original code: Jeffrey Way (https://gist.github.com/JeffreyWay/5112282)
* Modified for Laravel 5 and to use closure: soufiane EL HAMCHI (https://gist.github.com/soufianeEL/3f8483f0f3dc9e3ec5d9)
* and Ferri Sutanto (https://gist.github.com/ghprod/0bb7f8d207ba7838a0e6)
* Uses meta csrfToken and meta csrfParam based on: https://github.com/rails/jquery-ujs/blob/master/src/rails.js
*
* Examples :
* <a href="posts/2" data-method="delete">
* - Or, request confirmation in the process -
* <a href="posts/2" data-method="delete" data-confirm="Are you sure?">
*
* Uses CSRF token defined in your <head> tag. For Laravel 5, use the following code in a blade template :
* <meta name="csrf-token" content="{{ csrf_token() }}">
* <meta name="csrf-param" content="_token">
*/
(function(window, $, undefined) {
var Laravel = {
initialize: function() {
this.methodLinks = $('a[data-method]');
this.registerEvents();
},
// Up-to-date Cross-Site Request Forgery token
csrfToken: function() {
return $('meta[name=csrf-token]').attr('content');
},
// URL param that must contain the CSRF token
csrfParam: function() {
return $('meta[name=csrf-param]').attr('content');
},
registerEvents: function() {
this.methodLinks.on('click', this.handleMethod);
},
handleMethod: function(e) {
e.preventDefault();
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 false;
}
Laravel
.verifyConfirm(link)
.done(function () {
form = Laravel.createForm(link)
form.submit()
});
},
verifyConfirm: function(link) {
var confirm = new $.Deferred();
var userResponse = window.confirm(link.data('confirm'))
if (userResponse) {
confirm.resolve(link)
} else {
confirm.reject(link)
}
return confirm.promise();
},
createForm: function(link) {
var form =
$('<form>', {
'method': 'POST',
'action': link.attr('href')
});
var csrfToken = Laravel.csrfToken();
var csrfParam = Laravel.csrfParam();
if (csrfParam !== undefined && csrfToken !== undefined) {
var token = $('<input>', {
'name': csrfParam,
'value': csrfToken,
'type': 'hidden'
});
form.append(token);
}
var hiddenInput =
$('<input>', {
'name': '_method',
'value': link.data('method'),
'type': 'hidden'
});
return form.append(hiddenInput)
.appendTo('body');
}
};
Laravel.initialize();
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment