File tree Expand file tree Collapse file tree 3 files changed +57
-17
lines changed Expand file tree Collapse file tree 3 files changed +57
-17
lines changed Original file line number Diff line number Diff line change @@ -1276,17 +1276,26 @@ angular.module('schemaForm')
1276
1276
//make the form available to decorators
1277
1277
scope . schemaForm = { form : merged , schema : schema } ;
1278
1278
1279
- //Create directives from the form definition
1280
- angular . forEach ( merged , function ( obj , i ) {
1281
- var n = document . createElement ( attrs . sfDecoratorName ||
1282
- snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
1283
- n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
1284
- frag . appendChild ( n ) ;
1285
- } ) ;
1286
-
1287
1279
//clean all but pre existing html.
1288
1280
element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
1289
1281
1282
+ //Create directives from the form definition
1283
+ angular . forEach ( merged , function ( obj , i ) {
1284
+ var n = document . createElement ( attrs . sfDecorator || snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
1285
+ n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
1286
+ var slot ;
1287
+ try {
1288
+ slot = element [ 0 ] . querySelector ( '*[sf-insert-field="' + obj . key + '"]' ) ;
1289
+ } catch ( err ) {
1290
+ slot = null ;
1291
+ }
1292
+ if ( slot ) {
1293
+ slot . appendChild ( n ) ;
1294
+ } else {
1295
+ frag . appendChild ( n ) ;
1296
+ }
1297
+ } ) ;
1298
+
1290
1299
element [ 0 ] . appendChild ( frag ) ;
1291
1300
1292
1301
//compile only children
Original file line number Diff line number Diff line change @@ -80,17 +80,27 @@ angular.module('schemaForm')
80
80
//make the form available to decorators
81
81
scope . schemaForm = { form : merged , schema : schema } ;
82
82
83
- //Create directives from the form definition
84
- angular . forEach ( merged , function ( obj , i ) {
85
- var n = document . createElement ( attrs . sfDecoratorName ||
86
- snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
87
- n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
88
- frag . appendChild ( n ) ;
89
- } ) ;
90
-
91
83
//clean all but pre existing html.
92
84
element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
93
85
86
+ //Create directives from the form definition
87
+ angular . forEach ( merged , function ( obj , i ) {
88
+ var n = document . createElement ( attrs . sfDecorator || snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
89
+ n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
90
+ var slot ;
91
+ try {
92
+ slot = element [ 0 ] . querySelector ( '*[sf-insert-field="' + obj . key + '"]' ) ;
93
+ } catch ( err ) {
94
+ // field insertion not supported for complex keys
95
+ slot = null ;
96
+ }
97
+ if ( slot ) {
98
+ slot . appendChild ( n ) ;
99
+ } else {
100
+ frag . appendChild ( n ) ;
101
+ }
102
+ } ) ;
103
+
94
104
element [ 0 ] . appendChild ( frag ) ;
95
105
96
106
//compile only children
Original file line number Diff line number Diff line change @@ -173,6 +173,27 @@ describe('directive',function(){
173
173
} ) ;
174
174
} ) ;
175
175
176
+ it ( 'should preserve existing html and insert fields in matching slots' , function ( ) {
177
+
178
+ inject ( function ( $compile , $rootScope ) {
179
+ var scope = $rootScope . $new ( ) ;
180
+ scope . person = { } ;
181
+
182
+ scope . schema = exampleSchema ;
183
+
184
+ scope . form = [ "*" ] ;
185
+
186
+ var tmpl = angular . element ( '<form sf-schema="schema" sf-form="form" sf-model="person"><ul><li sf-insert-field="name"></li></ul></form>' ) ;
187
+
188
+ $compile ( tmpl ) ( scope ) ;
189
+ $rootScope . $apply ( ) ;
190
+
191
+ tmpl . children ( ) . eq ( 0 ) . is ( 'ul' ) . should . be . true ;
192
+ tmpl . children ( ) . eq ( 0 ) . find ( 'input' ) . attr ( 'ng-model' ) . should . be . equal ( 'model[\'name\']' ) ;
193
+ } ) ;
194
+ } ) ;
195
+
196
+
176
197
it ( 'should handle submit buttons' , function ( ) {
177
198
178
199
inject ( function ( $compile , $rootScope ) {
@@ -1599,4 +1620,4 @@ describe('directive',function(){
1599
1620
} ) ;
1600
1621
} ) ;
1601
1622
1602
- } ) ;
1623
+ } ) ;
You can’t perform that action at this time.
0 commit comments