@@ -95,29 +95,54 @@ angular.module('angular-json-editor', []).provider('JSONEditor', function () {
95
95
throw new Error ( 'angular-json-editor: could not resolve schema data.' ) ;
96
96
}
97
97
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
+ }
102
104
103
- var editor = scope . editor ;
105
+ scope . editor = new JSONEditor ( element [ 0 ] , {
106
+ startval : values ,
107
+ schema : schema
108
+ } , true ) ;
104
109
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
+ }
108
114
109
- editor . on ( 'change' , function ( ) {
115
+ function editorReady ( ) {
116
+ scope . isValid = ( scope . editor . validate ( ) . length === 0 ) ;
117
+ }
118
+
119
+ function editorChange ( ) {
110
120
// Fire the onChange callback
111
121
if ( typeof scope . onChange === 'function' ) {
112
122
scope . onChange ( {
113
- $editorValue : editor . getValue ( )
123
+ $editorValue : scope . editor . getValue ( )
114
124
} ) ;
115
125
}
116
126
// reset isValid property onChange
117
127
scope . $apply ( function ( ) {
118
- scope . isValid = ( editor . validate ( ) . length === 0 ) ;
128
+ scope . isValid = ( scope . editor . validate ( ) . length === 0 ) ;
119
129
} ) ;
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 ) ;
121
146
122
147
// Transclude the buttons at the bottom.
123
148
var buttons = transclude ( scope , function ( clone ) {
0 commit comments