5454*
5555*/
5656
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 ;
6165var oldSet = proto . __set ;
6266var ErrorsObj ;
6367var defaultValidationOpts ;
@@ -70,7 +74,7 @@ var resolveComputes = function (itemObj, opts) {
7074 var processedObj = { } ;
7175
7276 // Loop through each validation option
73- can . each ( opts , function ( item , key ) {
77+ canEach ( opts , function ( item , key ) {
7478 var actualOpts = item ;
7579 if ( typeof item === 'function' ) {
7680 // create compute and add it to computes array
@@ -102,9 +106,9 @@ var getPropDefineBehavior = function (behavior, attr, define) {
102106} ;
103107
104108// Default Map for errors object. Useful to add instance helpers
105- ErrorsObj = can . Map . extend ( { } , {
109+ ErrorsObj = canMap . extend ( { } , {
106110 hasErrors : function ( ) {
107- return ! can . isEmptyObject ( this . attr ( ) ) ;
111+ return ! isEmptyObject ( this . attr ( ) ) ;
108112 }
109113} ) ;
110114
@@ -134,12 +138,12 @@ var initProperty = function (key, value) {
134138 mapValidateCache = getValidateFromCache . call ( this ) ;
135139
136140 // 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 ] ) ) {
138142 validateOpts = mapValidateCache [ key ] ;
139143 propIniting = false ;
140144 } else {
141145 // 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 ) ) ;
143147 // Need to build computes in the next step
144148 propIniting = true ;
145149 }
@@ -148,7 +152,7 @@ var initProperty = function (key, value) {
148152 if ( typeof validateOpts !== 'undefined' ) {
149153 //create validation computes only when initing the map
150154 if ( propIniting ) {
151- validateOpts = can . extend ( { } ,
155+ validateOpts = deepAssign ( { } ,
152156 defaultValidationOpts ,
153157 validateOpts ,
154158 // Find any functions, converts them to computes and returns
@@ -177,11 +181,11 @@ proto.init = function () {
177181 oldInit . apply ( this , arguments ) ;
178182 }
179183} ;
180- can . extend ( can . Map . prototype , {
184+ deepAssign ( canMap . prototype , {
181185 _initValidation : function ( ) {
182186 var self = this ;
183187 var validateCache = getValidateFromCache . call ( this ) ;
184- can . each ( this . define , function ( props , key ) {
188+ canEach ( this . define , function ( props , key ) {
185189 if ( props . validate && ! validateCache [ key ] ) {
186190 initProperty . call ( self , key , self [ key ] ) ;
187191 }
@@ -211,18 +215,18 @@ can.extend(can.Map.prototype, {
211215 var self = this ;
212216
213217 // Loop through validate options
214- can . each ( this . define , function ( value , key ) {
218+ canEach ( this . define , function ( value , key ) {
215219 if ( value . validate ) {
216220 processedOpts [ key ] = resolveComputes ( { key : key , value : self . attr ( key ) } , validateOpts [ key ] ) ;
217221 }
218222 } ) ;
219- var errors = can . validate . validate ( this . serialize ( ) , processedOpts ) ;
223+ var errors = canValidate . validate ( this . serialize ( ) , processedOpts ) ;
220224
221225 // Process errors if we got them
222226 // TODO: This creates a new instance every time.
223227 this . attr ( 'errors' , new ErrorsObj ( errors ) ) ;
224228
225- return can . isEmptyObject ( errors ) ;
229+ return isEmptyObject ( errors ) ;
226230 } ,
227231 /**
228232 * @function _validateOne Validate One
@@ -235,16 +239,17 @@ can.extend(can.Map.prototype, {
235239 *
236240 * @param {object } item A key/value object
237241 * @param {object } opts Object that contains validation config.
242+ + @param {object} otherItems Object that contains other attributes in the map
238243 * @return {boolean } True if method found that the property can be saved; if
239244 * validation fails and the property must validate (`mustValidate` property),
240245 * this will be `false`.
241246 */
242- _validateOne : function ( item , opts ) {
247+ _validateOne : function ( item , opts , otherItems ) {
243248 var errors ;
244249 var allowSet = true ;
245250
246251 // 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 ) ;
248253
249254 // Process errors if we got them
250255 if ( errors && errors . length > 0 ) {
@@ -287,11 +292,11 @@ can.extend(can.Map.prototype, {
287292 var self = this ;
288293
289294 // Loop through each validation option
290- can . each ( opts , function ( item , key ) {
295+ canEach ( opts , function ( item , key ) {
291296 processedObj [ key ] = item ;
292297 if ( typeof item === 'function' ) {
293298 // 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 ) ) ;
295300 computes . push ( { key : key , compute : compute } ) ;
296301 processedObj [ key ] = compute ;
297302 }
@@ -300,10 +305,10 @@ can.extend(can.Map.prototype, {
300305 // Using the computes array, create necessary listeners
301306 // We do this afterwards instead of inline so we can have access
302307 // to the final set of validation options.
303- can . each ( computes , function ( item ) {
308+ canEach ( computes , function ( item ) {
304309 item . compute . bind ( 'change' , function ( ) {
305310 itemObj . value = self . attr ( itemObj . key ) ;
306- self . _validateOne ( itemObj , processedObj ) ;
311+ self . _validateOne ( itemObj , processedObj , self . attr ( ) ) ;
307312 } ) ;
308313 } ) ;
309314
@@ -325,7 +330,7 @@ proto.__set = function (prop, value, current, success, error) {
325330 // If validate opts are set and initing, validate properties only if validateOnInit is true
326331 if ( ( validateOpts && ! mapIniting ) || ( validateOpts && mapIniting && validateOpts . validateOnInit ) ) {
327332 // Validate item
328- allowSet = this . _validateOne ( { key : prop , value : value } , validateOpts ) ;
333+ allowSet = this . _validateOne ( { key : prop , value : value } , validateOpts , this . attr ( ) ) ;
329334 }
330335 }
331336
0 commit comments