forked from tungstenfabric/tf-webui-third-party
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-validation-amd.patch
204 lines (191 loc) · 9.22 KB
/
backbone-validation-amd.patch
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
diff -Naur old/backbone-validation-amd.js new/backbone-validation-amd.js
--- old/backbone-validation-amd.js 2014-03-28 14:45:08.000000000 -0700
+++ new/backbone-validation-amd.js 2014-12-19 12:44:12.000000000 -0800
@@ -97,8 +97,8 @@
// Returns an object with undefined properties for all
// attributes on the model that has defined one or more
// validation rules.
- var getValidatedAttrs = function(model) {
- return _.reduce(_.keys(_.result(model, 'validation') || {}), function(memo, key) {
+ var getValidatedAttrs = function(model, validationName) {
+ return _.reduce(_.keys(_.result(model, validationName) || {}), function(memo, key) {
memo[key] = void 0;
return memo;
}, {});
@@ -107,8 +107,8 @@
// Looks on the model for validations for a specified
// attribute. Returns an array of any validators defined,
// or an empty array if none is defined.
- var getValidators = function(model, attr) {
- var attrValidationSet = model.validation ? _.result(model, 'validation')[attr] || {} : {};
+ var getValidators = function(model, attr, validation) {
+ var attrValidationSet = _.result(model, validation) ? _.result(model, validation)[attr] || {} : {};
// If the validator is a function or a string, wrap it in a function validator
if (_.isFunction(attrValidationSet) || _.isString(attrValidationSet)) {
@@ -141,16 +141,16 @@
// for that attribute. If one or more errors are found,
// the first error message is returned.
// If the attribute is valid, an empty string is returned.
- var validateAttr = function(model, attr, value, computed) {
+ var validateAttr = function(model, attr, value, computed, validation) {
// Reduces the array of validators to an error message by
// applying all the validators and returning the first error
// message, if any.
- return _.reduce(getValidators(model, attr), function(memo, validator){
+ return _.reduce(getValidators(model, attr, validation), function(memo, validator){
// Pass the format functions plus the default
// validators as the context to the validator
var ctx = _.extend({}, formatFunctions, defaultValidators),
result = validator.fn.call(ctx, value, attr, validator.val, model, computed);
-
+
if(result === false || memo === false) {
return false;
}
@@ -164,15 +164,15 @@
// Loops through the model's attributes and validates them all.
// Returns and object containing names of invalid attributes
// as well as error messages.
- var validateModel = function(model, attrs) {
+ var validateModel = function(model, attrs, validation) {
var error,
invalidAttrs = {},
isValid = true,
computed = _.clone(attrs),
flattened = flatten(attrs);
-
+
_.each(flattened, function(val, attr) {
- error = validateAttr(model, attr, val, computed);
+ error = validateAttr(model, attr, val, computed, validation);
if (error) {
invalidAttrs[attr] = error;
isValid = false;
@@ -191,9 +191,10 @@
// Check whether or not a value, or a hash of values
// passes validation without updating the model
- preValidate: function(attr, value) {
+ preValidate: function(attr, value, validationName) {
var self = this,
result = {},
+ validation = validationName == null ? 'validation' : validationName,
error;
if(_.isObject(attr)){
@@ -207,43 +208,45 @@
return _.isEmpty(result) ? undefined : result;
}
else {
- return validateAttr(this, attr, value, _.extend({}, this.attributes));
+ return validateAttr(this, attr, value, _.extend({}, this.attributes), validation);
}
},
// Check to see if an attribute, an array of attributes or the
// entire model is valid. Passing true will force a validation
// of the model.
- isValid: function(option) {
- var flattened = flatten(this.attributes);
-
+ isValid: function(option, validationName) {
+ var flattened = flatten(this.attributes),
+ validation = validationName == null ? 'validation' : validationName;
+
if(_.isString(option)){
- return !validateAttr(this, option, flattened[option], _.extend({}, this.attributes));
+ return validateAttr(this, option, flattened[option], _.extend({}, this.attributes), validation);
}
if(_.isArray(option)){
return _.reduce(option, function(memo, attr) {
- return memo && !validateAttr(this, attr, flattened[attr], _.extend({}, this.attributes));
+ return memo && !validateAttr(this, attr, flattened[attr], _.extend({}, this.attributes), validation);
}, true, this);
}
if(option === true) {
- this.validate();
+ this.validate(null, null, validationName);
}
- return this.validation ? this._isValid : true;
+ return _.result(this, validation) ? this._isValid : true;
},
// This is called by Backbone when it needs to perform validation.
// You can call it manually without any parameters to validate the
// entire model.
- validate: function(attrs, setOptions){
+ validate: function(attrs, setOptions, validationName){
var model = this,
validateAll = !attrs,
opt = _.extend({}, options, setOptions),
- validatedAttrs = getValidatedAttrs(model),
+ validatedAttrs = getValidatedAttrs(model, validationName),
allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs),
changedAttrs = flatten(attrs || allAttrs),
+ validation = validationName == null ? 'validation' : validationName,
- result = validateModel(model, allAttrs);
-
+ result = validateModel(model, allAttrs, validation);
+
model._isValid = result.isValid;
// After validation is performed, loop through all validated attributes
@@ -262,7 +265,7 @@
changed = changedAttrs.hasOwnProperty(attr);
if(invalid && (changed || validateAll)){
- opt.invalid(view, attr, result.invalidAttrs[attr], opt.selector);
+ opt.invalid(view, attr, result.invalidAttrs[attr], opt.selector, validation);
}
});
@@ -299,13 +302,13 @@
// Mix in validation on a model whenever a model is
// added to a collection
var collectionAdd = function(model) {
- bindModel(this.view, model, this.options);
+ bindModel(this.view, model.attributes.model(), this.options);
};
// Remove validation from a model whenever a model is
// removed from a collection
var collectionRemove = function(model) {
- unbindModel(model);
+ unbindModel(model.attributes.model());
};
// Returns the public methods on Backbone.Validation
@@ -324,20 +327,20 @@
bind: function(view, options) {
options = _.extend({}, defaultOptions, defaultCallbacks, options);
- var model = options.model || view.model,
+ var model = view.model.model(),
collection = options.collection || view.collection;
-
- if(typeof model === 'undefined' && typeof collection === 'undefined'){
+
+ if(typeof view.model === 'undefined' && typeof collection === 'undefined'){
throw 'Before you execute the binding your view must have a model or a collection.\n' +
'See http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.';
}
- if(model) {
+ if(view.model !== 'undefined' && model) {
bindModel(view, model, options);
}
- else if(collection) {
+ if(typeof collection !== 'undefined' && collection) {
collection.each(function(model){
- bindModel(view, model, options);
+ bindModel(view, model.attributes.model(), options);
});
collection.bind('add', collectionAdd, {view: view, options: options});
collection.bind('remove', collectionRemove);
@@ -350,13 +353,13 @@
options = _.extend({}, options);
var model = options.model || view.model,
collection = options.collection || view.collection;
-
- if(model) {
+
+ if(view.model !== 'undefined' && model) {
unbindModel(model);
}
- else if(collection) {
+ if(typeof collection !== 'undefined' && collection) {
collection.each(function(model){
- unbindModel(model);
+ unbindModel(model.attributes.model());
});
collection.unbind('add', collectionAdd);
collection.unbind('remove', collectionRemove);