1
- angular . module ( 'schemaForm' ) . directive ( 'schemaValidate' , [ 'sfValidator' , 'sfSelect' , function ( sfValidator , sfSelect ) {
1
+ angular . module ( 'schemaForm' ) . directive ( 'schemaValidate' , [ 'sfValidator' , 'sfSelect' , 'sfUnselect' ,
2
+ function ( sfValidator , sfSelect , sfUnselect ) {
3
+
2
4
return {
3
5
restrict : 'A' ,
4
6
scope : false ,
@@ -8,7 +10,6 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
8
10
require : 'ngModel' ,
9
11
link : function ( scope , element , attrs , ngModel ) {
10
12
11
-
12
13
// We need the ngModelController on several places,
13
14
// most notably for errors.
14
15
// So we emit it up to the decorator directive so it can put it on scope.
@@ -101,6 +102,68 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
101
102
102
103
} ) ;
103
104
105
+
106
+ var DEFAULT_DESTROY_STRATEGY ;
107
+ if ( scope . options && scope . options . formDefaults ) {
108
+ var formDefaultDestroyStrategy = scope . options . formDefaults . destroyStrategy ;
109
+ var isValidFormDefaultDestroyStrategy = ( formDefaultDestroyStrategy === undefined ||
110
+ formDefaultDestroyStrategy === '' ||
111
+ formDefaultDestroyStrategy === null ||
112
+ formDefaultDestroyStrategy === 'retain' ) ;
113
+ if ( isValidFormDefaultDestroyStrategy ) {
114
+ DEFAULT_DESTROY_STRATEGY = formDefaultDestroyStrategy ;
115
+ }
116
+ else {
117
+ console . warn ( 'Unrecognized formDefaults.destroyStrategy: \'%s\'. Used undefined instead.' ,
118
+ formDefaultDestroyStrategy ) ;
119
+ DEFAULT_DESTROY_STRATEGY = undefined ;
120
+ }
121
+ }
122
+
123
+ // Clean up the model when the corresponding form field is $destroy-ed.
124
+ // Default behavior can be supplied as a formDefault, and behavior can be overridden in the form definition.
125
+ scope . $on ( '$destroy' , function ( ) {
126
+ var form = getForm ( ) ;
127
+ var destroyStrategy = form . destroyStrategy ; // Either set in form definition, or as part of formDefaults.
128
+ var schemaType = getSchemaType ( ) ;
129
+
130
+ if ( destroyStrategy && destroyStrategy !== 'retain' ) {
131
+ // Don't recognize the strategy, so give a warning.
132
+ console . warn ( 'Unrecognized destroyStrategy: \'%s\'. Used default instead.' , destroyStrategy ) ;
133
+ destroyStrategy = DEFAULT_DESTROY_STRATEGY ;
134
+ }
135
+ else if ( schemaType !== 'string' && destroyStrategy === '' ) {
136
+ // Only 'string' type fields can have an empty string value as a valid option.
137
+ console . warn ( 'Attempted to use empty string destroyStrategy on non-string form type. Used default instead.' ) ;
138
+ destroyStrategy = DEFAULT_DESTROY_STRATEGY ;
139
+ }
140
+
141
+ if ( destroyStrategy === 'retain' ) {
142
+ return ; // Valid option to avoid destroying data in the model.
143
+ }
144
+
145
+ destroyUsingStrategy ( destroyStrategy ) ;
146
+
147
+ function destroyUsingStrategy ( strategy ) {
148
+ var strategyIsDefined = ( strategy === null || strategy === '' || typeof strategy == undefined ) ;
149
+ if ( ! strategyIsDefined ) {
150
+ strategy = DEFAULT_DESTROY_STRATEGY ;
151
+ }
152
+ sfUnselect ( scope . form . key , scope . model , strategy ) ;
153
+ }
154
+
155
+ function getSchemaType ( ) {
156
+ if ( form . schema ) {
157
+ schemaType = form . schema . type ;
158
+ }
159
+ else {
160
+ schemaType = null ;
161
+ }
162
+ }
163
+ } ) ;
164
+
165
+
166
+
104
167
scope . schemaError = function ( ) {
105
168
return error ;
106
169
} ;
0 commit comments