@@ -19,6 +19,7 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
19
19
link : function ( scope , element , attrs , ngModel ) {
20
20
var formDefCache = { } ;
21
21
22
+ scope . validateArray = angular . noop ;
22
23
23
24
if ( ngModel ) {
24
25
// We need the ngModelController on several places,
@@ -41,9 +42,8 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
41
42
// We only modify the same array instance but someone might change the array from
42
43
// the outside so let's watch for that. We use an ordinary watch since the only case
43
44
// we're really interested in is if its a new instance.
44
- scope . $watch ( 'model' + sfPath . normalize ( form . key ) , function ( ) {
45
- list = sfSelect ( form . key , scope . model ) ;
46
- scope . modelArray = list ;
45
+ scope . $watch ( 'model' + sfPath . normalize ( form . key ) , function ( value ) {
46
+ scope . modelArray = value ;
47
47
} ) ;
48
48
49
49
// Since ng-model happily creates objects in a deep path when setting a
@@ -126,19 +126,15 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
126
126
}
127
127
128
128
// Trigger validation.
129
- if ( scope . validateArray ) {
130
- scope . validateArray ( ) ;
131
- }
129
+ scope . validateArray ( ) ;
132
130
return list ;
133
131
} ;
134
132
135
133
scope . deleteFromArray = function ( index ) {
136
134
list . splice ( index , 1 ) ;
137
135
138
136
// Trigger validation.
139
- if ( scope . validateArray ) {
140
- scope . validateArray ( ) ;
141
- }
137
+ scope . validateArray ( ) ;
142
138
143
139
// Angular 1.2 lacks setDirty
144
140
if ( ngModel && ngModel . $setDirty ) {
@@ -172,29 +168,29 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
172
168
form . titleMap . forEach ( function ( item ) {
173
169
scope . titleMapValues . push ( arr . indexOf ( item . value ) !== - 1 ) ;
174
170
} ) ;
175
-
176
171
} ;
177
172
//Catch default values
178
173
updateTitleMapValues ( scope . modelArray ) ;
179
174
scope . $watchCollection ( 'modelArray' , updateTitleMapValues ) ;
180
175
181
176
//To get two way binding we also watch our titleMapValues
182
- scope . $watchCollection ( 'titleMapValues' , function ( vals ) {
183
- if ( vals ) {
177
+ scope . $watchCollection ( 'titleMapValues' , function ( vals , old ) {
178
+ if ( vals && vals !== old ) {
184
179
var arr = scope . modelArray ;
185
180
186
181
// Apparently the fastest way to clear an array, readable too.
187
182
// http://jsperf.com/array-destroy/32
188
183
while ( arr . length > 0 ) {
189
184
arr . pop ( ) ;
190
185
}
191
-
192
186
form . titleMap . forEach ( function ( item , index ) {
193
187
if ( vals [ index ] ) {
194
188
arr . push ( item . value ) ;
195
189
}
196
190
} ) ;
197
191
192
+ // Time to validate the rebuilt array.
193
+ scope . validateArray ( ) ;
198
194
}
199
195
} ) ;
200
196
}
0 commit comments