1 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
2 and is published under the AGPL license. */
4 // wrapper around Form.request that sets up the submit listener, stops the
5 // submit event, calls presubmit function, calls Form.request and calls a
7 Element.addMethods('form', {ajaxize : function(form, options) {
8 var reqoptions, timeout;
10 options = Object.clone(options);
11 reqoptions = Object.clone(options);
14 function onSuccess(transport, json) {
16 window.clearTimeout(timeout);
19 if (transport.getStatus() === 0) {
20 options.onFailure(transport, json);
22 options.onSuccess(transport, json);
26 function onFailure(transport, json) {
28 window.clearTimeout(timeout);
31 options.onFailure(transport, json);
34 delete(reqoptions.presubmit);
35 delete(reqoptions.postsubmit);
37 $(form).observe('submit', function(evt) {
40 evt.stop(); // cancel form submission
41 if (Object.isFunction(options.presubmit)) {
42 if (options.presubmit(this) === false) {
46 req = this.request(Object.extend(reqoptions, {
50 timeout = (function() {
51 options.onFailure(null);
53 }).delay(options.timeout || 20);
54 if (Object.isFunction(options.postsubmit)) {
55 options.postsubmit(this);