@@ -129,28 +129,52 @@ function(sel, sfPath, schemaForm) {
129129 } ) ;
130130
131131 scope . appendToArray = function ( ) {
132-
133132 var empty ;
134133
135- // Same old add empty things to the array hack :(
136- if ( scope . form && scope . form . schema ) {
137- if ( scope . form . schema . items ) {
138- if ( scope . form . schema . items . type === 'object' ) {
139- empty = { } ;
140- } else if ( scope . form . schema . items . type === 'array' ) {
141- empty = [ ] ;
142- }
143- }
144- }
145-
134+ // Create and set an array if needed.
146135 var model = scope . modelArray ;
147136 if ( ! model ) {
148- // Create and set an array if needed.
149137 var selection = sfPath . parse ( attrs . sfNewArray ) ;
150138 model = [ ] ;
151139 sel ( selection , scope , model ) ;
152140 scope . modelArray = model ;
153141 }
142+
143+ // Same old add empty things to the array hack :(
144+ if ( scope . form && scope . form . schema && scope . form . schema . items ) {
145+
146+ var items = scope . form . schema . items ;
147+ if ( items . type && items . type . indexOf ( 'object' ) !== - 1 ) {
148+ empty = { } ;
149+
150+ // Check for possible defaults
151+ if ( ! scope . options || scope . options . setSchemaDefaults !== false ) {
152+ empty = angular . isDefined ( items [ 'default' ] ) ? items [ 'default' ] : empty ;
153+
154+ // Check for defaults further down in the schema.
155+ // If the default instance sets the new array item to something falsy, i.e. null
156+ // then there is no need to go further down.
157+ if ( empty ) {
158+ schemaForm . traverseSchema ( items , function ( prop , path ) {
159+ if ( angular . isDefined ( prop [ 'default' ] ) ) {
160+ sel ( path , empty , prop [ 'default' ] ) ;
161+ }
162+ } ) ;
163+ }
164+ }
165+
166+ } else if ( items . type && items . type . indexOf ( 'array' ) !== - 1 ) {
167+ empty = [ ] ;
168+ if ( ! scope . options || scope . options . setSchemaDefaults !== false ) {
169+ empty = items [ 'default' ] || empty ;
170+ }
171+ } else {
172+ // No type? could still have defaults.
173+ if ( ! scope . options || scope . options . setSchemaDefaults !== false ) {
174+ empty = items [ 'default' ] || empty ;
175+ }
176+ }
177+ }
154178 model . push ( empty ) ;
155179
156180 return model ;
0 commit comments