-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (51 loc) · 1.51 KB
/
index.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
62
var resource = require('resource'),
forms = resource.define('forms'),
mschemaForms = require('mschema-forms');
var view = require('view');
forms.schema.description = "for generating HTML forms";
forms.method("generate", generate, {
"resource": {
"type": "object",
"required": true,
"default": {}
},
"data": {
"type": "object",
"required": false
}
});
forms.method('middleware', middleware);
function middleware (opts) {
opts = opts || {};
return function (req, res, next) {
var entity = opts.resource || 'unknown';
opts.method = opts.method || opts.view;
opts.params = req.resource.params;
view.create({ path: __dirname + '/view', input: "html" }, function (err, view) {
var str = '', form;
form = view.form[opts.method] || view.form['method'];
opts.action = opts.action || "/form";
form.present(opts, function (err, html) {
if (err) {
res.end(err.message);
return;
}
res.setHeader('Content-Type', 'text/html');
res.end(html);
});
});
};
};
function generate (options, callback) {
view.create({ path: __dirname + '/view', input: "html" }, function (err, view) {
var str = '', form;
form = view.form[options.method] || view.form[options.view];
if (typeof form === "undefined") {
return callback(new Error('no form found'));
}
form.present(options, function(err, res){
return callback(err, res);
});
});
};
module['exports'] = forms;