backbone-nested-models.js
5.04 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
define(
[
'underscore',
'backbone',
'backbone-validation',
],
function(
_,
Backbone
) {
'use strict';
function validateNestedValue(attrValue, attrName) {
attrValue.validate();
var isValid = attrValue.isValid();
return isValid ? null : (attrName + ' is invalid');
}
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 (value === 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(validateNestedValue);
}
}
}
model.validation = validation;
return oldValidation;
}
function wrapValidationFunction(modelClass, methodName) {
var originalMethod = modelClass.prototype[methodName];
modelClass.prototype[methodName] = function() {
var oldValidation = updateValidation(this);
try {
if (originalMethod) {
return originalMethod.apply(this, arguments);
} else {
return modelClass.__super__[methodName].apply(this, arguments);
}
} finally {
this.validation = oldValidation;
}
};
}
function wrapSetFunction(modelClass) {
var originalMethod = modelClass.prototype.set;
modelClass.prototype.set = function(key, val, options) {
var attr, attrs, curVal, nestedOptions, newVal;
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};
for (attr in attrs) {
curVal = this.get(attr);
newVal = attrs[attr];
if (curVal instanceof Backbone.Model && newVal instanceof Backbone.Model) {
delete attrs[attr];
curVal.set(newVal.attributes, nestedOptions);
}
}
}
if (originalMethod) {
return originalMethod.call(this, attrs, options);
} else {
return modelClass.__super__.set.call(this, attrs, options);
}
};
}
function wrapToJSONFunction(modelClass) {
var originalMethod = modelClass.prototype.toJSON;
modelClass.prototype.toJSON = function(options) {
var result;
if (originalMethod) {
result = originalMethod.apply(this, arguments);
} else {
result = modelClass.__super__.toJSON.apply(this, arguments);
}
if (options && options.deep) {
_.each(result, function(value, key) {
if (value instanceof Backbone.Model) {
result[key] = value.toJSON(options);
}
});
}
return result;
};
}
var NestedModels = {
validateNestedValue: validateNestedValue,
wrapSetFunction: wrapSetFunction,
wrapToJSONFunction: wrapToJSONFunction,
wrapValidationFunction: wrapValidationFunction,
mixin: function(modelClass) {
wrapSetFunction(modelClass);
wrapToJSONFunction(modelClass);
wrapValidationFunction(modelClass, 'isValid');
wrapValidationFunction(modelClass, 'validate');
wrapValidationFunction(modelClass, 'preValidate');
return modelClass;
},
};
return NestedModels;
}
);