Skip to content

Commit a2e6ef2

Browse files
committed
added support for updating schema using scope.
1 parent 7a46754 commit a2e6ef2

File tree

7 files changed

+90
-28
lines changed

7 files changed

+90
-28
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-json-editor",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"authors": [
55
"Rodik Hanukaev <[email protected]>",
66
"Topaz Bar <[email protected]>"

demo/app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ angular.module('demoApp', ['angular-json-editor']).config(function (JSONEditorPr
6262
console.log('onAction2');
6363
};
6464

65+
$scope.changeSchema = function () {
66+
$scope.schema.properties.height = {
67+
required: true,
68+
title: 'Height',
69+
type: 'integer'
70+
};
71+
};
72+
6573
}).controller('AsyncButtonsController', function ($scope) {
6674

6775
$scope.onSubmit = function () {

demo/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ <h2>Buttons Controller</h2>
3030
Action 2
3131
</button>
3232

33+
<button type="button" class="btn btn-primary" ng-click="changeSchema($event)">
34+
<span class="glyphicon glyphicon-plus"></span>
35+
Update Schema
36+
</button>
3337
</json-editor>
3438
</div>
3539

dist/angular-json-editor.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

dist/angular-json-editor.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-json-editor",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"repository": {
55
"type": "git",
66
"url": "git://github.com/rodikh/angular-json-editor.git"

src/angular-json-editor.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,54 @@ angular.module('angular-json-editor', []).provider('JSONEditor', function () {
9292
throw new Error('angular-json-editor: could not resolve schema data.');
9393
}
9494

95-
scope.editor = new JSONEditor(element[0], {
96-
startval: startVal,
97-
schema: schema
98-
});
95+
function restart() {
96+
var values = startVal;
97+
if (scope.editor && scope.editor.destroy) {
98+
values = scope.editor.getValue();
99+
scope.editor.destroy();
100+
}
99101

100-
var editor = scope.editor;
102+
scope.editor = new JSONEditor(element[0], {
103+
startval: values,
104+
schema: schema
105+
}, true);
101106

102-
editor.on('ready', function () {
103-
scope.isValid = (editor.validate().length === 0);
104-
});
107+
scope.editor.on('ready', editorReady);
108+
scope.editor.on('change', editorChange);
109+
element.append(buttons);
110+
}
105111

106-
editor.on('change', function () {
112+
function editorReady() {
113+
scope.isValid = (scope.editor.validate().length === 0);
114+
}
115+
116+
function editorChange() {
107117
// Fire the onChange callback
108118
if (typeof scope.onChange === 'function') {
109119
scope.onChange({
110-
$editorValue: editor.getValue()
120+
$editorValue: scope.editor.getValue()
111121
});
112122
}
113123
// reset isValid property onChange
114124
scope.$apply(function () {
115-
scope.isValid = (editor.validate().length === 0);
125+
scope.isValid = (scope.editor.validate().length === 0);
116126
});
117-
});
127+
}
128+
129+
restart(startVal, schema);
130+
131+
scope.$watch('schema', function (newVal, oldVal) {
132+
//update newScheme
133+
if (newVal.success) {
134+
newVal.success(function (data) {
135+
schema = data;
136+
});
137+
} else {
138+
schema = newVal;
139+
}
140+
141+
restart();
142+
}, true);
118143

119144
// Transclude the buttons at the bottom.
120145
var buttons = transclude(scope, function (clone) {

0 commit comments

Comments
 (0)