CodeOfficer (owner)

Revisions

gist: 145492 Download_button fork
public
Public Clone URL: git://gist.github.com/145492.git
Embed All Files: show embed
JavaScript #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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});
});