This repository has been archived by the owner on May 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
93 lines (73 loc) · 1.55 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Module dependencies.
*/
var Attribute = require('./attribute')
, Emitter = require('emitter');
/**
* Expose `Form`.
*/
module.exports = Form;
/**
* Initialize a new `Form` with a `schema` object
*
* @param {Object} schema
* @api public
*/
function Form(schema) {
if (!schema) throw Error('No schema provided');
this.schema = schema;
}
/**
* Mixin emitter.
*/
Emitter(Form.prototype);
/**
* Iterates through all attributes in `this.schema`
* and renders fields.
*
* @return {Form} self
* @api public
*/
Form.prototype.render = function() {
this.view = document.createElement('div');
this.view.className = 'form';
this.attributes = {};
for (var name in this.schema) {
var subSchema = this.schema[name]
, attribute = new Attribute(name, subSchema);
this.view.appendChild(attribute.render().view);
this.attributes[name] = attribute;
}
return this;
}
/**
* Iterates through all attributes in and
* gets their values;
*
* @return {Object} values
* @api public
*/
Form.prototype.getValue = function(){
var values = {}
for (var attr in this.attributes) {
var attribute = this.attributes[attr];
values[attr] = attribute.value();
}
return values;
}
/**
* Iterates through all attributes and
* set their values;
*
* @param {Object} data
* @return {Form} this
* @api public
*/
Form.prototype.setValue = function(data){
var values = {}
for (var attr in this.attributes) {
var attribute = this.attributes[attr];
values[attr] = attribute.value(data[attr]);
}
return values;
}