Skip to content

Instantly share code, notes, and snippets.

@CodeOfficer
Created July 12, 2009 02:24
Show Gist options
  • Save CodeOfficer/145492 to your computer and use it in GitHub Desktop.
Save CodeOfficer/145492 to your computer and use it in GitHub Desktop.
jQuery.fn.restForm = function(type, options) {
var defaults = {
method: 'post',
action: this.attr('href'),
confirm: false,
confirm_message: 'Are you sure?',
trigger_on: 'click'
};
var opts = $.extend(defaults, options);
this.bind(opts.trigger_on, function() {
var $form = $('<form></form>').attr({
method: opts.method,
action: opts.action
}).append('<input type="hidden" name="_method" value="' + type + '" />');
if (opts.confirm && !confirm(opts.confirm_message)) {
return false;
}
this.after($form);
$form.submit();
return false;
});
};
$(function() {
$('.put').restForm('PUT');
$('.delete').restForm('DELETE', {confirm: true});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment