54
54
*
55
55
*/
56
56
57
- import can from 'can' ;
58
- import 'can-validate/can-validate' ;
59
-
60
- var proto = can . Map . prototype ;
57
+ import canMap from 'can-map' ;
58
+ import canCompute from 'can-compute' ;
59
+ import canValidate from 'can-validate/can-validate' ;
60
+ import canEach from 'can-util/js/each/' ;
61
+ import isEmptyObject from 'can-util/js/is-empty-object/' ;
62
+ import deepAssign from 'can-util/js/deep-assign/' ;
63
+
64
+ var proto = canMap . prototype ;
61
65
var oldSet = proto . __set ;
62
66
var ErrorsObj ;
63
67
var defaultValidationOpts ;
@@ -70,7 +74,7 @@ var resolveComputes = function (itemObj, opts) {
70
74
var processedObj = { } ;
71
75
72
76
// Loop through each validation option
73
- can . each ( opts , function ( item , key ) {
77
+ canEach ( opts , function ( item , key ) {
74
78
var actualOpts = item ;
75
79
if ( typeof item === 'function' ) {
76
80
// create compute and add it to computes array
@@ -102,9 +106,9 @@ var getPropDefineBehavior = function (behavior, attr, define) {
102
106
} ;
103
107
104
108
// Default Map for errors object. Useful to add instance helpers
105
- ErrorsObj = can . Map . extend ( { } , {
109
+ ErrorsObj = canMap . extend ( { } , {
106
110
hasErrors : function ( ) {
107
- return ! can . isEmptyObject ( this . attr ( ) ) ;
111
+ return ! isEmptyObject ( this . attr ( ) ) ;
108
112
}
109
113
} ) ;
110
114
@@ -134,12 +138,12 @@ var initProperty = function (key, value) {
134
138
mapValidateCache = getValidateFromCache . call ( this ) ;
135
139
136
140
// If validate options don't exist in cache for current prop, create them
137
- if ( mapValidateCache [ key ] && ! can . isEmptyObject ( mapValidateCache [ key ] ) ) {
141
+ if ( mapValidateCache [ key ] && ! isEmptyObject ( mapValidateCache [ key ] ) ) {
138
142
validateOpts = mapValidateCache [ key ] ;
139
143
propIniting = false ;
140
144
} else {
141
145
// Copy current prop's validation properties to cache
142
- validateOpts = can . extend ( { } , getPropDefineBehavior ( 'validate' , key , this . define ) ) ;
146
+ validateOpts = deepAssign ( { } , getPropDefineBehavior ( 'validate' , key , this . define ) ) ;
143
147
// Need to build computes in the next step
144
148
propIniting = true ;
145
149
}
@@ -148,7 +152,7 @@ var initProperty = function (key, value) {
148
152
if ( typeof validateOpts !== 'undefined' ) {
149
153
//create validation computes only when initing the map
150
154
if ( propIniting ) {
151
- validateOpts = can . extend ( { } ,
155
+ validateOpts = deepAssign ( { } ,
152
156
defaultValidationOpts ,
153
157
validateOpts ,
154
158
// Find any functions, converts them to computes and returns
@@ -177,11 +181,11 @@ proto.init = function () {
177
181
oldInit . apply ( this , arguments ) ;
178
182
}
179
183
} ;
180
- can . extend ( can . Map . prototype , {
184
+ deepAssign ( canMap . prototype , {
181
185
_initValidation : function ( ) {
182
186
var self = this ;
183
187
var validateCache = getValidateFromCache . call ( this ) ;
184
- can . each ( this . define , function ( props , key ) {
188
+ canEach ( this . define , function ( props , key ) {
185
189
if ( props . validate && ! validateCache [ key ] ) {
186
190
initProperty . call ( self , key , self [ key ] ) ;
187
191
}
@@ -211,18 +215,18 @@ can.extend(can.Map.prototype, {
211
215
var self = this ;
212
216
213
217
// Loop through validate options
214
- can . each ( this . define , function ( value , key ) {
218
+ canEach ( this . define , function ( value , key ) {
215
219
if ( value . validate ) {
216
220
processedOpts [ key ] = resolveComputes ( { key : key , value : self . attr ( key ) } , validateOpts [ key ] ) ;
217
221
}
218
222
} ) ;
219
- var errors = can . validate . validate ( this . serialize ( ) , processedOpts ) ;
223
+ var errors = canValidate . validate ( this . serialize ( ) , processedOpts ) ;
220
224
221
225
// Process errors if we got them
222
226
// TODO: This creates a new instance every time.
223
227
this . attr ( 'errors' , new ErrorsObj ( errors ) ) ;
224
228
225
- return can . isEmptyObject ( errors ) ;
229
+ return isEmptyObject ( errors ) ;
226
230
} ,
227
231
/**
228
232
* @function _validateOne Validate One
@@ -235,16 +239,17 @@ can.extend(can.Map.prototype, {
235
239
*
236
240
* @param {object } item A key/value object
237
241
* @param {object } opts Object that contains validation config.
242
+ + @param {object} otherItems Object that contains other attributes in the map
238
243
* @return {boolean } True if method found that the property can be saved; if
239
244
* validation fails and the property must validate (`mustValidate` property),
240
245
* this will be `false`.
241
246
*/
242
- _validateOne : function ( item , opts ) {
247
+ _validateOne : function ( item , opts , otherItems ) {
243
248
var errors ;
244
249
var allowSet = true ;
245
250
246
251
// run validation
247
- errors = can . validate . once ( item . value , can . extend ( { } , opts ) , item . key ) ;
252
+ errors = canValidate . once ( item . value , deepAssign ( { } , opts ) , item . key , otherItems ) ;
248
253
249
254
// Process errors if we got them
250
255
if ( errors && errors . length > 0 ) {
@@ -287,11 +292,11 @@ can.extend(can.Map.prototype, {
287
292
var self = this ;
288
293
289
294
// Loop through each validation option
290
- can . each ( opts , function ( item , key ) {
295
+ canEach ( opts , function ( item , key ) {
291
296
processedObj [ key ] = item ;
292
297
if ( typeof item === 'function' ) {
293
298
// create compute and add it to computes array
294
- var compute = can . compute ( can . proxy ( item , self ) ) ;
299
+ var compute = canCompute ( Function . prototype . bind . call ( item , self ) ) ;
295
300
computes . push ( { key : key , compute : compute } ) ;
296
301
processedObj [ key ] = compute ;
297
302
}
@@ -300,10 +305,10 @@ can.extend(can.Map.prototype, {
300
305
// Using the computes array, create necessary listeners
301
306
// We do this afterwards instead of inline so we can have access
302
307
// to the final set of validation options.
303
- can . each ( computes , function ( item ) {
308
+ canEach ( computes , function ( item ) {
304
309
item . compute . bind ( 'change' , function ( ) {
305
310
itemObj . value = self . attr ( itemObj . key ) ;
306
- self . _validateOne ( itemObj , processedObj ) ;
311
+ self . _validateOne ( itemObj , processedObj , self . attr ( ) ) ;
307
312
} ) ;
308
313
} ) ;
309
314
@@ -325,7 +330,7 @@ proto.__set = function (prop, value, current, success, error) {
325
330
// If validate opts are set and initing, validate properties only if validateOnInit is true
326
331
if ( ( validateOpts && ! mapIniting ) || ( validateOpts && mapIniting && validateOpts . validateOnInit ) ) {
327
332
// Validate item
328
- allowSet = this . _validateOne ( { key : prop , value : value } , validateOpts ) ;
333
+ allowSet = this . _validateOne ( { key : prop , value : value } , validateOpts , this . attr ( ) ) ;
329
334
}
330
335
}
331
336
0 commit comments