backbone-nested-models.js
5.9 KB
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
define(
[
'underscore',
'backbone',
'backbone-validation',
],
function(
_,
Backbone
) {
'use strict';
function extractNestedErrors(errors) {
var result = {};
for (var key in errors) {
var value = errors[key];
if (value.errors) {
value = extractNestedErrors(value.errors);
}
result[key] = value;
}
return result;
}
function validateNestedValue(attrValue, attrName) {
var errors = attrValue.validate();
var isValid = attrValue.isValid();
if (isValid) {
return null;
}
var r = {};
r.toString = function() {
return attrName + ' is invalid';
};
r.errors = errors;
return r;
}
function updateValidation(model) {
var oldValidation = model.validation;
var allKeys = _.uniq(model.keys().concat(_.keys(oldValidation)));
var validation = _.extend({}, oldValidation);
var found;
var f = function(value) {
if (typeof value === 'object' && value.fn === validateNestedValue) {
found = true;
}
};
for (var i = 0; i < allKeys.length; i++) {
var key = allKeys[i];
var value = model.get(key);
var validators = validation[key];
if (validators) {
if(_.isArray(validators)) {
validators = validators.concat();
} else {
validators = [validators];
}
} else {
validators = [];
}
validation[key] = validators;
if (value instanceof Backbone.Model) {
found = false;
_.each(validators, f);
if (!found) {
validators.push({fn: validateNestedValue});
}
}
}
model.validation = validation;
return oldValidation;
}
function wrapValidationFunction(modelClass, methodName) {
var originalMethod = modelClass.prototype[methodName];
modelClass.prototype[methodName] = function() {
var oldValidation = updateValidation(this);
try {
return originalMethod.apply(this, arguments);
} finally {
this.validation = oldValidation;
}
};
return modelClass;
}
function wrapSetFunction(modelClass) {
var originalMethod = modelClass.prototype.set;
modelClass.prototype.set = function(key, val, options) {
var attr, attrs, curVal, nestedOptions, newVal;
if (typeof(key) === 'undefined') {
return this;
}
if (key === null) {
return this;
}
if (typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
if (options && options.merge) {
nestedOptions = {silent: false, merge: true, parse: options.parse};
for (attr in attrs) {
curVal = this.get(attr);
newVal = attrs[attr];
if (curVal instanceof Backbone.Model) {
if (newVal instanceof Backbone.Model) {
delete attrs[attr];
curVal.set(newVal.attributes, nestedOptions);
} else if (options.parse && typeof newVal === 'object') {
delete attrs[attr];
curVal.set(newVal, nestedOptions);
}
}
}
}
return originalMethod.call(this, attrs, options);
};
return modelClass;
}
function wrapToJSONFunction(modelClass) {
var originalMethod = modelClass.prototype.toJSON;
modelClass.prototype.toJSON = function(options) {
var result = originalMethod.apply(this, arguments);
if (options && options.deep) {
_.each(result, function(value, key) {
if (value instanceof Backbone.Model) {
result[key] = value.toJSON(options);
}
});
}
return result;
};
return modelClass;
}
var NestedModels = {
extractNestedErrors: extractNestedErrors,
validateNestedValue: validateNestedValue,
wrapSetFunction: wrapSetFunction,
wrapToJSONFunction: wrapToJSONFunction,
wrapValidationFunction: wrapValidationFunction,
wrapValidationFunctions: function(modelClass) {
modelClass = wrapValidationFunction(modelClass, 'isValid');
modelClass = wrapValidationFunction(modelClass, 'validate');
modelClass = wrapValidationFunction(modelClass, 'preValidate');
return modelClass;
},
mixin: function(modelClass) {
modelClass = NestedModels.wrapSetFunction(modelClass);
modelClass = NestedModels.wrapToJSONFunction(modelClass);
modelClass = NestedModels.wrapValidationFunctions(modelClass);
return modelClass;
},
};
return NestedModels;
}
);