-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathform.js
69 lines (51 loc) · 1.62 KB
/
form.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
63
64
65
66
67
68
var Events = require('github/jxmono/events');
var data = require('./data');
var ui = require('./ui');
function setTemplate (template, callback) {
var self = this;
callback = typeof callback === 'function' ? callback : function () {};
var template = typeof template === 'string' ? template : (template.id || template._id);
// check template
if (!template) {
// TODO handle error
alert('Invalid template');
return callback('Invalid template');
}
self.emit('find', [template], function (err, templates) {
for (var key in templates) {
if (!templates.hasOwnProperty(key)) continue;
if (templates[key]._id === template) {
self.template = templates[key];
}
}
// TODO handle error
if (err || !self.template) {
return callback(err);
}
// set current template
self.emit('templateSet');
callback(null, self.template);
});
}
function init (config) {
var self = this;
self.config = config;
config.binds = config.binds || [];
config.options = config.options || {};
config.options.dataChanged = config.options.dataChanged || "change";
if (!config.waitFor) {
return console.error('No crud miid defined.');
}
// init ui
if (self.config.ui) {
ui.call(self);
}
// init data events
data.call(self);
// set template
self.on('setTemplate', setTemplate);
// listen to external events
Events.call(self, config);
self.emit('ready');
}
module.exports = init;