@@ -1308,88 +1308,107 @@ angular.module('schemaForm')
1308
1308
}
1309
1309
}
1310
1310
} ) ;
1311
- //Since we are dependant on up to three
1312
- //attributes we'll do a common watch
1311
+
1313
1312
var lastDigest = { } ;
1314
1313
var childScope ;
1315
- scope . $watch ( function ( ) {
1316
1314
1317
- var schema = scope . schema ;
1318
- var form = scope . initialForm || [ '*' ] ;
1315
+ // Common renderer function, can either be triggered by a watch or by an event.
1316
+ var render = function ( schema , form ) {
1317
+ var merged = schemaForm . merge ( schema , form , ignore , scope . options ) ;
1318
+ var frag = document . createDocumentFragment ( ) ;
1319
+
1320
+ // Create a new form and destroy the old one.
1321
+ // Not doing keeps old form elements hanging around after
1322
+ // they have been removed from the DOM
1323
+ // https://github.com/Textalk/angular-schema-form/issues/200
1324
+ if ( childScope ) {
1325
+ childScope . $destroy ( ) ;
1326
+ }
1327
+ childScope = scope . $new ( ) ;
1319
1328
1320
- //The check for schema.type is to ensure that schema is not {}
1321
- if ( form && schema && schema . type &&
1322
- ( lastDigest . form !== form || lastDigest . schema !== schema ) &&
1323
- Object . keys ( schema . properties ) . length > 0 ) {
1324
- lastDigest . schema = schema ;
1325
- lastDigest . form = form ;
1329
+ //make the form available to decorators
1330
+ childScope . schemaForm = { form : merged , schema : schema } ;
1326
1331
1327
- var merged = schemaForm . merge ( schema , form , ignore , scope . options ) ;
1328
- var frag = document . createDocumentFragment ( ) ;
1332
+ //clean all but pre existing html.
1333
+ element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
1329
1334
1330
- // Create a new form and destroy the old one.
1331
- // Not doing keeps old form elements hanging around after
1332
- // they have been removed from the DOM
1333
- // https://github.com/Textalk/angular-schema-form/issues/200
1334
- if ( childScope ) {
1335
- childScope . $destroy ( ) ;
1335
+ // Find all slots.
1336
+ var slots = { } ;
1337
+ var slotsFound = element [ 0 ] . querySelectorAll ( '*[sf-insert-field]' ) ;
1338
+
1339
+ for ( var i = 0 ; i < slotsFound . length ; i ++ ) {
1340
+ slots [ slotsFound [ i ] . getAttribute ( 'sf-insert-field' ) ] = slotsFound [ i ] ;
1341
+ }
1342
+
1343
+ //Create directives from the form definition
1344
+ angular . forEach ( merged , function ( obj , i ) {
1345
+ var n = document . createElement ( attrs . sfDecorator ||
1346
+ snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
1347
+ n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
1348
+
1349
+ // Check if there is a slot to put this in...
1350
+ if ( obj . key ) {
1351
+ var slot = slots [ sfPath . stringify ( obj . key ) ] ;
1352
+ if ( slot ) {
1353
+ while ( slot . firstChild ) {
1354
+ slot . removeChild ( slot . firstChild ) ;
1355
+ }
1356
+ slot . appendChild ( n ) ;
1357
+ return ;
1358
+ }
1336
1359
}
1337
- childScope = scope . $new ( ) ;
1338
1360
1339
- //make the form available to decorators
1340
- childScope . schemaForm = { form : merged , schema : schema } ;
1361
+ // ...otherwise add it to the frag
1362
+ frag . appendChild ( n ) ;
1363
+
1364
+ } ) ;
1341
1365
1342
- //clean all but pre existing html.
1343
- element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
1366
+ element [ 0 ] . appendChild ( frag ) ;
1344
1367
1345
- // Find all slots.
1346
- var slots = { } ;
1347
- var slotsFound = element [ 0 ] . querySelectorAll ( '*[sf-insert-field]' ) ;
1368
+ //compile only children
1369
+ $compile ( element . children ( ) ) ( childScope ) ;
1348
1370
1349
- for ( var i = 0 ; i < slotsFound . length ; i ++ ) {
1350
- slots [ slotsFound [ i ] . getAttribute ( 'sf-insert-field' ) ] = slotsFound [ i ] ;
1371
+ //ok, now that that is done let's set any defaults
1372
+ schemaForm . traverseSchema ( schema , function ( prop , path ) {
1373
+ if ( angular . isDefined ( prop [ 'default' ] ) ) {
1374
+ var val = sfSelect ( path , scope . model ) ;
1375
+ if ( angular . isUndefined ( val ) ) {
1376
+ sfSelect ( path , scope . model , prop [ 'default' ] ) ;
1377
+ }
1351
1378
}
1379
+ } ) ;
1352
1380
1353
- //Create directives from the form definition
1354
- angular . forEach ( merged , function ( obj , i ) {
1355
- var n = document . createElement ( attrs . sfDecorator ||
1356
- snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
1357
- n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
1358
-
1359
- // Check if there is a slot to put this in...
1360
- if ( obj . key ) {
1361
- var slot = slots [ sfPath . stringify ( obj . key ) ] ;
1362
- if ( slot ) {
1363
- while ( slot . firstChild ) {
1364
- slot . removeChild ( slot . firstChild ) ;
1365
- }
1366
- slot . appendChild ( n ) ;
1367
- return ;
1368
- }
1369
- }
1381
+ scope . $emit ( 'sf-render-finished' , element ) ;
1382
+ } ;
1370
1383
1371
- // ...otherwise add it to the frag
1372
- frag . appendChild ( n ) ;
1384
+ //Since we are dependant on up to three
1385
+ //attributes we'll do a common watch
1386
+ scope . $watch ( function ( ) {
1373
1387
1374
- } ) ;
1388
+ var schema = scope . schema ;
1389
+ var form = scope . initialForm || [ '*' ] ;
1375
1390
1376
- element [ 0 ] . appendChild ( frag ) ;
1391
+ //The check for schema.type is to ensure that schema is not {}
1392
+ if ( form && schema && schema . type &&
1393
+ ( lastDigest . form !== form || lastDigest . schema !== schema ) &&
1394
+ Object . keys ( schema . properties ) . length > 0 ) {
1395
+ lastDigest . schema = schema ;
1396
+ lastDigest . form = form ;
1377
1397
1378
- //compile only children
1379
- $compile ( element . children ( ) ) ( childScope ) ;
1398
+ render ( schema , form ) ;
1399
+ }
1400
+ } ) ;
1380
1401
1381
- //ok, now that that is done let's set any defaults
1382
- schemaForm . traverseSchema ( schema , function ( prop , path ) {
1383
- if ( angular . isDefined ( prop [ 'default' ] ) ) {
1384
- var val = sfSelect ( path , scope . model ) ;
1385
- if ( angular . isUndefined ( val ) ) {
1386
- sfSelect ( path , scope . model , prop [ 'default' ] ) ;
1387
- }
1388
- }
1389
- } ) ;
1390
- } ;
1391
- scope . $emit ( 'sf-render-finished' , element ) ;
1402
+ // We also listen to the event schemaFormRedraw so you can manually trigger a change if
1403
+ // part of the form or schema is chnaged without it being a new instance.
1404
+ scope . $on ( 'schemaFormRedraw' , function ( ) {
1405
+ var schema = scope . schema ;
1406
+ var form = scope . initialForm || [ '*' ] ;
1407
+ if ( schema ) {
1408
+ render ( schema , form ) ;
1409
+ }
1392
1410
} ) ;
1411
+
1393
1412
}
1394
1413
} ;
1395
1414
}
0 commit comments