11/**
22 * Directive that handles the model arrays
33 */
4- angular . module ( 'schemaForm' ) . directive ( 'sfNewArray' , [ 'sfSelect' , 'sfPath' , function ( sel , sfPath ) {
4+ angular . module ( 'schemaForm' ) . directive ( 'sfNewArray' , [ 'sfSelect' , 'sfPath' , 'schemaForm' ,
5+ function ( sel , sfPath , schemaForm ) {
56 return {
67 scope : false ,
78 link : function ( scope , element , attrs ) {
@@ -24,10 +25,10 @@ angular.module('schemaForm').directive('sfNewArray', ['sfSelect', 'sfPath', func
2425
2526 var onChangeFn = function ( ) {
2627 if ( scope . form && scope . form . onChange ) {
27- if ( angular . isFunction ( form . onChange ) ) {
28- form . onChange ( ctrl . $modelValue , form ) ;
28+ if ( angular . isFunction ( scope . form . onChange ) ) {
29+ scope . form . onChange ( scope . modelArray , scope . form ) ;
2930 } else {
30- scope . evalExpr ( form . onChange , { 'modelValue' : ctrl . $modelValue , form : form } ) ;
31+ scope . evalExpr ( scope . form . onChange , { 'modelValue' : scope . modelArray , form : scope . form } ) ;
3132 }
3233 }
3334 } ;
@@ -162,6 +163,49 @@ angular.module('schemaForm').directive('sfNewArray', ['sfSelect', 'sfPath', func
162163 }
163164 return model ;
164165 } ;
166+
167+ // For backwards compatability, i.e. when a bootstrap-decorator tag is used
168+ // as child to the array.
169+ var setIndex = function ( index ) {
170+ return function ( form ) {
171+ if ( form . key ) {
172+ form . key [ form . key . indexOf ( '' ) ] = index ;
173+ }
174+ } ;
175+ } ;
176+ var formDefCache = { } ;
177+ scope . copyWithIndex = function ( index ) {
178+ var form = scope . form ;
179+ if ( ! formDefCache [ index ] ) {
180+
181+ // To be more compatible with JSON Form we support an array of items
182+ // in the form definition of "array" (the schema just a value).
183+ // for the subforms code to work this means we wrap everything in a
184+ // section. Unless there is just one.
185+ var subForm = form . items [ 0 ] ;
186+ if ( form . items . length > 1 ) {
187+ subForm = {
188+ type : 'section' ,
189+ items : form . items . map ( function ( item ) {
190+ item . ngModelOptions = form . ngModelOptions ;
191+ if ( angular . isUndefined ( item . readonly ) ) {
192+ item . readonly = form . readonly ;
193+ }
194+ return item ;
195+ } )
196+ } ;
197+ }
198+
199+ if ( subForm ) {
200+ var copy = angular . copy ( subForm ) ;
201+ copy . arrayIndex = index ;
202+ schemaForm . traverseForm ( copy , setIndex ( index ) ) ;
203+ formDefCache [ index ] = copy ;
204+ }
205+ }
206+ return formDefCache [ index ] ;
207+ } ;
208+
165209 }
166210 } ;
167211} ] ) ;
0 commit comments