forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.js
61 lines (55 loc) · 1.84 KB
/
hooks.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
59
60
61
Hooks = {
form: {},
global: {
before: {},
after: {},
formToDoc: [],
docToForm: [],
onSubmit: [],
onSuccess: [],
onError: [],
beginSubmit: [],
endSubmit: []
}
};
Hooks.addHooksToList = function addHooksToList(hooksList, hooks, replace) {
// Add before hooks
hooks.before && _.each(hooks.before, function autoFormBeforeHooksEach(func, type) {
if (typeof func !== "function") {
throw new Error("AutoForm before hook must be a function, not " + typeof func);
}
hooksList.before[type] = (!replace && hooksList.before[type]) ? hooksList.before[type] : [];
hooksList.before[type].push(func);
});
// Add after hooks
hooks.after && _.each(hooks.after, function autoFormAfterHooksEach(func, type) {
if (typeof func !== "function") {
throw new Error("AutoForm after hook must be a function, not " + typeof func);
}
hooksList.after[type] = (!replace && hooksList.after[type]) ? hooksList.after[type] : [];
hooksList.after[type].push(func);
});
// Add all other hooks
_.each(['formToDoc', 'docToForm', 'onSubmit', 'onSuccess', 'onError', 'beginSubmit', 'endSubmit'], function autoFormHooksEach(name) {
if (hooks[name]) {
if (typeof hooks[name] !== "function") {
throw new Error("AutoForm " + name + " hook must be a function, not " + typeof hooks[name]);
}
if(replace) {
hooksList[name] = [];
}
hooksList[name].push(hooks[name]);
}
});
};
Hooks.getHooks = function getHooks(formId, type, subtype) {
var f, g;
if (subtype) {
f = Hooks.form[formId] && Hooks.form[formId][type] && Hooks.form[formId][type][subtype] || [];
g = Hooks.global[type] && Hooks.global[type][subtype] || [];
} else {
f = Hooks.form[formId] && Hooks.form[formId][type] || [];
g = Hooks.global[type] || [];
}
return f.concat(g);
};