@@ -95,29 +95,54 @@ angular.module('angular-json-editor', []).provider('JSONEditor', function () {
9595 throw new Error ( 'angular-json-editor: could not resolve schema data.' ) ;
9696 }
9797
98- scope . editor = new JSONEditor ( element [ 0 ] , {
99- startval : startVal ,
100- schema : schema
101- } ) ;
98+ function restart ( ) {
99+ var values = startVal ;
100+ if ( scope . editor && scope . editor . destroy ) {
101+ values = scope . editor . getValue ( ) ;
102+ scope . editor . destroy ( ) ;
103+ }
102104
103- var editor = scope . editor ;
105+ scope . editor = new JSONEditor ( element [ 0 ] , {
106+ startval : values ,
107+ schema : schema
108+ } , true ) ;
104109
105- editor . on ( 'ready' , function ( ) {
106- scope . isValid = ( editor . validate ( ) . length === 0 ) ;
107- } ) ;
110+ scope . editor . on ( 'ready' , editorReady ) ;
111+ scope . editor . on ( 'change' , editorChange ) ;
112+ element . append ( buttons ) ;
113+ }
108114
109- editor . on ( 'change' , function ( ) {
115+ function editorReady ( ) {
116+ scope . isValid = ( scope . editor . validate ( ) . length === 0 ) ;
117+ }
118+
119+ function editorChange ( ) {
110120 // Fire the onChange callback
111121 if ( typeof scope . onChange === 'function' ) {
112122 scope . onChange ( {
113- $editorValue : editor . getValue ( )
123+ $editorValue : scope . editor . getValue ( )
114124 } ) ;
115125 }
116126 // reset isValid property onChange
117127 scope . $apply ( function ( ) {
118- scope . isValid = ( editor . validate ( ) . length === 0 ) ;
128+ scope . isValid = ( scope . editor . validate ( ) . length === 0 ) ;
119129 } ) ;
120- } ) ;
130+ }
131+
132+ restart ( startVal , schema ) ;
133+
134+ scope . $watch ( 'schema' , function ( newVal , oldVal ) {
135+ //update newScheme
136+ if ( newVal . success ) {
137+ newVal . success ( function ( data ) {
138+ schema = data ;
139+ } ) ;
140+ } else {
141+ schema = newVal ;
142+ }
143+
144+ restart ( ) ;
145+ } , true ) ;
121146
122147 // Transclude the buttons at the bottom.
123148 var buttons = transclude ( scope , function ( clone ) {
0 commit comments