@@ -58,88 +58,107 @@ angular.module('schemaForm')
5858 }
5959 }
6060 } ) ;
61- //Since we are dependant on up to three
62- //attributes we'll do a common watch
61+
6362 var lastDigest = { } ;
6463 var childScope ;
65- scope . $watch ( function ( ) {
6664
67- var schema = scope . schema ;
68- var form = scope . initialForm || [ '*' ] ;
65+ // Common renderer function, can either be triggered by a watch or by an event.
66+ var render = function ( schema , form ) {
67+ var merged = schemaForm . merge ( schema , form , ignore , scope . options ) ;
68+ var frag = document . createDocumentFragment ( ) ;
69+
70+ // Create a new form and destroy the old one.
71+ // Not doing keeps old form elements hanging around after
72+ // they have been removed from the DOM
73+ // https://github.com/Textalk/angular-schema-form/issues/200
74+ if ( childScope ) {
75+ childScope . $destroy ( ) ;
76+ }
77+ childScope = scope . $new ( ) ;
6978
70- //The check for schema.type is to ensure that schema is not {}
71- if ( form && schema && schema . type &&
72- ( lastDigest . form !== form || lastDigest . schema !== schema ) &&
73- Object . keys ( schema . properties ) . length > 0 ) {
74- lastDigest . schema = schema ;
75- lastDigest . form = form ;
79+ //make the form available to decorators
80+ childScope . schemaForm = { form : merged , schema : schema } ;
81+
82+ //clean all but pre existing html.
83+ element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
7684
77- var merged = schemaForm . merge ( schema , form , ignore , scope . options ) ;
78- var frag = document . createDocumentFragment ( ) ;
85+ // Find all slots.
86+ var slots = { } ;
87+ var slotsFound = element [ 0 ] . querySelectorAll ( '*[sf-insert-field]' ) ;
7988
80- // Create a new form and destroy the old one.
81- // Not doing keeps old form elements hanging around after
82- // they have been removed from the DOM
83- // https://github.com/Textalk/angular-schema-form/issues/200
84- if ( childScope ) {
85- childScope . $destroy ( ) ;
89+ for ( var i = 0 ; i < slotsFound . length ; i ++ ) {
90+ slots [ slotsFound [ i ] . getAttribute ( 'sf-insert-field' ) ] = slotsFound [ i ] ;
91+ }
92+
93+ //Create directives from the form definition
94+ angular . forEach ( merged , function ( obj , i ) {
95+ var n = document . createElement ( attrs . sfDecorator ||
96+ snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
97+ n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
98+
99+ // Check if there is a slot to put this in...
100+ if ( obj . key ) {
101+ var slot = slots [ sfPath . stringify ( obj . key ) ] ;
102+ if ( slot ) {
103+ while ( slot . firstChild ) {
104+ slot . removeChild ( slot . firstChild ) ;
105+ }
106+ slot . appendChild ( n ) ;
107+ return ;
108+ }
86109 }
87- childScope = scope . $new ( ) ;
88110
89- //make the form available to decorators
90- childScope . schemaForm = { form : merged , schema : schema } ;
111+ // ...otherwise add it to the frag
112+ frag . appendChild ( n ) ;
91113
92- //clean all but pre existing html.
93- element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
114+ } ) ;
94115
95- // Find all slots.
96- var slots = { } ;
97- var slotsFound = element [ 0 ] . querySelectorAll ( '*[sf-insert-field]' ) ;
116+ element [ 0 ] . appendChild ( frag ) ;
98117
99- for ( var i = 0 ; i < slotsFound . length ; i ++ ) {
100- slots [ slotsFound [ i ] . getAttribute ( 'sf-insert-field' ) ] = slotsFound [ i ] ;
101- }
118+ //compile only children
119+ $compile ( element . children ( ) ) ( childScope ) ;
102120
103- //Create directives from the form definition
104- angular . forEach ( merged , function ( obj , i ) {
105- var n = document . createElement ( attrs . sfDecorator ||
106- snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
107- n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
108-
109- // Check if there is a slot to put this in...
110- if ( obj . key ) {
111- var slot = slots [ sfPath . stringify ( obj . key ) ] ;
112- if ( slot ) {
113- while ( slot . firstChild ) {
114- slot . removeChild ( slot . firstChild ) ;
115- }
116- slot . appendChild ( n ) ;
117- return ;
118- }
121+ //ok, now that that is done let's set any defaults
122+ schemaForm . traverseSchema ( schema , function ( prop , path ) {
123+ if ( angular . isDefined ( prop [ 'default' ] ) ) {
124+ var val = sfSelect ( path , scope . model ) ;
125+ if ( angular . isUndefined ( val ) ) {
126+ sfSelect ( path , scope . model , prop [ 'default' ] ) ;
119127 }
128+ }
129+ } ) ;
130+
131+ scope . $emit ( 'sf-render-finished' , element ) ;
132+ } ;
120133
121- // ...otherwise add it to the frag
122- frag . appendChild ( n ) ;
134+ //Since we are dependant on up to three
135+ //attributes we'll do a common watch
136+ scope . $watch ( function ( ) {
123137
124- } ) ;
138+ var schema = scope . schema ;
139+ var form = scope . initialForm || [ '*' ] ;
125140
126- element [ 0 ] . appendChild ( frag ) ;
141+ //The check for schema.type is to ensure that schema is not {}
142+ if ( form && schema && schema . type &&
143+ ( lastDigest . form !== form || lastDigest . schema !== schema ) &&
144+ Object . keys ( schema . properties ) . length > 0 ) {
145+ lastDigest . schema = schema ;
146+ lastDigest . form = form ;
127147
128- //compile only children
129- $compile ( element . children ( ) ) ( childScope ) ;
148+ render ( schema , form ) ;
149+ }
150+ } ) ;
130151
131- //ok, now that that is done let's set any defaults
132- schemaForm . traverseSchema ( schema , function ( prop , path ) {
133- if ( angular . isDefined ( prop [ 'default' ] ) ) {
134- var val = sfSelect ( path , scope . model ) ;
135- if ( angular . isUndefined ( val ) ) {
136- sfSelect ( path , scope . model , prop [ 'default' ] ) ;
137- }
138- }
139- } ) ;
140- } ;
141- scope . $emit ( 'sf-render-finished' , element ) ;
152+ // We also listen to the event schemaFormRedraw so you can manually trigger a change if
153+ // part of the form or schema is chnaged without it being a new instance.
154+ scope . $on ( 'schemaFormRedraw' , function ( ) {
155+ var schema = scope . schema ;
156+ var form = scope . initialForm || [ '*' ] ;
157+ if ( schema ) {
158+ render ( schema , form ) ;
159+ }
142160 } ) ;
161+
143162 }
144163 } ;
145164 }
0 commit comments