-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
177 lines (144 loc) · 3.59 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Module dependencies.
*/
var object = require('object'),
each = require('each'),
value = require('value'),
query = require('query'),
attr = require('attr'),
type = require('type'),
bind = require('bind');
/**
* Module exports.
*/
module.exports = function(handlers){
return function(Model){
// inject new method
Model.prototype.syncWith = function(el){
SyncModel(el, this, handlers);
};
};
};
/**
* Create/initialize a new synchronizer instance.
*/
function SyncModel(el, model, handlers) {
if (!(this instanceof SyncModel))
return new SyncModel(el, model, handlers);
var self = this;
this.typeHandlers = {
'integer': bind(this, this.toNumber),
'number': bind(this, this.toNumber)
};
this.formatHandlers = handlers || {};
this.el = el;
this.model = model;
each(model.model.attrs, function (key, meta) {
self.sync(key, meta);
});
return model;
};
/**
* Synchronize the model at `key` with the data from the DOM.
*
* @param {String} key
* @param {Object} schema
*/
SyncModel.prototype.sync = function (key, schema) {
// dispatch the the handling to the appropriate function
if (!schema) return;
switch (schema.type) {
case 'object': return this.syncObject(key, schema);
case 'array': return this.syncArray(key, schema);
default: return this.syncValue(key, schema);
}
};
/**
* Synchronize a single value.
*
* @param {String} key the key to the value
* @param {Object} schema the schema of the affected attr
* @param {Object} value
*/
SyncModel.prototype.syncValue = function(key, schema){
var el,
val,
handler = null;
el = query('[name=' + JSON.stringify(key) + ']', this.el);
if (!el) return;
val = value(el);
if (val === '') val = undefined;
// apply type handler if available
handler = this.typeHandlers[schema.type];
if (handler) val = handler(val);
// apply format handler if available
if (schema.format) {
handler = this.formatHandlers[schema.format];
if (handler) val = handler(val);
}
// update the modified value
this.updateValue(key, val);
};
/**
* Synchronize an object.
*
* @param {String} key the key to the object
* @param {Object} schema the schema of the affected attr
*/
SyncModel.prototype.syncObject = function(key, schema){
var self = this;
each(schema.properties, function (k, v) {
self.sync(key + '.' + k, v);
});
};
/**
* Synchronize an array.
*
* @param {String} key the key to the array
* @param {Object} schema the schema of the affected attr
*/
SyncModel.prototype.syncArray = function(key, schema){
var queryFn;
// function to generate the next query string
queryFn = function(i){
return '[name^=' + JSON.stringify(key + '.' + i) + ']';
};
// set initial to empty array
this.updateValue(key, []);
for (var i = 0; query(queryFn(i), this.el); ++i) {
this.sync(key + '.' + i, schema.items);
}
};
/**
* Converts the value to a number.
*/
SyncModel.prototype.toNumber = function(value){
return Number(value);
};
/**
* Update the value at `key`.
*
* @param {String} key
* @param {Object} val
*/
SyncModel.prototype.updateValue = function (key, val) {
var obj,
part,
i,
keyParts = key.split('.');
obj = this.model[keyParts[0]]();
if (obj === undefined)
obj = {};
if (keyParts.length === 1) {
obj = val;
} else {
part = obj;
for (i = 1; i < keyParts.length-1; i++) {
if (part[keyParts[i]] === undefined)
part[keyParts[i]] = {};
part = part[keyParts[i]];
}
part[keyParts[keyParts.length-1]] = val;
}
this.model[keyParts[0]](obj);
};