-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradual.js
58 lines (51 loc) · 1.29 KB
/
gradual.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
var emitter = require('contra/emitter');
var formium = require('formium');
var formulario = require('formulario');
var formResponseHandler = require('./lib/formResponseHandler');
var state = require('./lib/state');
var transformers = [];
var gradual = emitter({
hijack: hijack,
transform: formium.transform,
submit: submit,
configure: state.configure
});
function noop () {}
function hijack (e) {
var form = e.target;
submitForm(form);
e.preventDefault();
}
function submitForm (form, done) {
formium.submit(form, handler);
function handler (err, data) {
raise(err, data, form, done);
}
}
function submit (options, done) {
var form = options.form;
if (form) {
submitForm(form, done); return;
}
var formData = formulario(options.data);
var req = {
method: options.method,
headers: formData.headers,
body: formData.body
};
return state.taunus.xhr(options.action, req, handler);
function handler (err, data) {
raise(err, data, null, done);
}
}
function raise (err, data, context, done) {
if (err) {
gradual.emit.call(context, 'error', err);
} else {
gradual.emit.call(context, 'data', data);
}
gradual.emit.call(context, 'response', err, data);
formResponseHandler(err, data, context, done);
}
module.exports = gradual;