Skip to content

Instantly share code, notes, and snippets.

@stevemo
Forked from JeffreyWay/laravel.js
Created March 7, 2013 22:29
Show Gist options
  • Save stevemo/5112425 to your computer and use it in GitHub Desktop.
Save stevemo/5112425 to your computer and use it in GitHub Desktop.
/*
<a href="posts/2" data-method="delete"> <---- We want to send an HTTP DELETE request
*/
(function() {
var laravel = {
initialize: function() {
this.methodLinks = $('a[data-method]');
this.registerEvents();
},
registerEvents: function() {
this.methodLinks.on('click', this.handleMethod);
},
handleMethod: function(e) {
var link = $(this);
var httpMethod = link.data('method').toUpperCase();
var allowedMethods = ['PUT', 'DELETE'];
// If the data-method attribute is not PUT or DELETE,
// then we don't know what to do. Just ignore.
if ( $.inArray(httpMethod, allowedMethods) === - 1 ) {
return;
}
var form =
$('<form>', {
'method': 'POST',
'action': link.attr('href')
});
var hiddenInput =
$('<input>', {
'name': '_method',
'type': 'hidden',
'value': link.data('method')
});
form.append(hiddenInput)
.appendTo('body')
.submit();
e.preventDefault();
}
};
laravel.initialize();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment