Skip to content

Commit 5cb68dd

Browse files
committed
Fix bug with object properties named "type". Fixes #183
Add setOption method (only works for `show_errors` currently). For #242 Make window.jsoneditor accessible from demo.html to help with debugging.
1 parent a4c17cf commit 5cb68dd

File tree

8 files changed

+67
-20
lines changed

8 files changed

+67
-20
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": "json-editor",
3-
"version": "0.7.11",
3+
"version": "0.7.12",
44
"authors": [
55
"Jeremy Dorn <[email protected]>"
66
],

demo.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ <h2>Code</h2>
275275
schema: schema,
276276
startval: startval
277277
});
278+
window.jsoneditor = jsoneditor;
278279

279280
// When the value of the editor changes, update the JSON output and validation message
280281
jsoneditor.on('change',function() {

dist/jsoneditor.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*! JSON Editor v0.7.11 - JSON Schema -> HTML Editor
1+
/*! JSON Editor v0.7.12 - JSON Schema -> HTML Editor
22
* By Jeremy Dorn - https://github.com/jdorn/json-editor/
33
* Released under the MIT license
44
*
5-
* Date: 2014-09-28
5+
* Date: 2014-10-05
66
*/
77

88
/**
@@ -307,6 +307,8 @@ JSONEditor.prototype = {
307307
this.callbacks = this.callbacks || {};
308308
this.callbacks[event] = this.callbacks[event] || [];
309309
this.callbacks[event].push(callback);
310+
311+
return this;
310312
},
311313
off: function(event, callback) {
312314
// Specific callback
@@ -329,13 +331,29 @@ JSONEditor.prototype = {
329331
else {
330332
this.callbacks = {};
331333
}
334+
335+
return this;
332336
},
333337
trigger: function(event) {
334338
if(this.callbacks && this.callbacks[event] && this.callbacks[event].length) {
335339
for(var i=0; i<this.callbacks[event].length; i++) {
336340
this.callbacks[event][i]();
337341
}
338342
}
343+
344+
return this;
345+
},
346+
setOption: function(option, value) {
347+
if(option === "show_errors") {
348+
this.options.show_errors = value;
349+
this.onChange();
350+
}
351+
// Only the `show_errors` option is supported for now
352+
else {
353+
throw "Option "+option+" must be set during instantiation and cannot be changed later";
354+
}
355+
356+
return this;
339357
},
340358
getEditorClass: function(schema) {
341359
var classname;
@@ -378,10 +396,15 @@ JSONEditor.prototype = {
378396
if(self.options.show_errors !== "never") {
379397
self.root.showValidationErrors(self.validation_results);
380398
}
399+
else {
400+
self.root.showValidationErrors([]);
401+
}
381402

382403
// Fire change event
383404
self.trigger('change');
384405
});
406+
407+
return this;
385408
},
386409
compileTemplate: function(template, name) {
387410
name = name || JSONEditor.defaults.template;
@@ -687,10 +710,10 @@ JSONEditor.prototype = {
687710
}, []);
688711
}
689712
// Type should be intersected and is either an array or string
690-
else if(prop === 'type') {
713+
else if(prop === 'type' && (typeof val === "string" || Array.isArray(val))) {
691714
// Make sure we're dealing with arrays
692-
if(typeof val !== "object") val = [val];
693-
if(typeof obj2.type !== "object") obj2.type = [obj2.type];
715+
if(typeof val === "string") val = [val];
716+
if(typeof obj2.type === "string") obj2.type = [obj2.type];
694717

695718

696719
extended.type = val.filter(function(n) {
@@ -2104,9 +2127,9 @@ JSONEditor.defaults.editors.string = JSONEditor.AbstractEditor.extend({
21042127
var self = this;
21052128

21062129
if(this.jsoneditor.options.show_errors === "always") {}
2107-
else if(!this.is_dirty) return;
2108-
2130+
else if(!this.is_dirty && this.previous_error_setting===this.jsoneditor.options.show_errors) return;
21092131

2132+
this.previous_error_setting = this.jsoneditor.options.show_errors;
21102133

21112134
var messages = [];
21122135
$each(errors,function(i,error) {

dist/jsoneditor.min.js

Lines changed: 4 additions & 4 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
@@ -2,7 +2,7 @@
22
"name": "json-editor",
33
"title": "JSONEditor",
44
"description": "JSON Schema based editor",
5-
"version": "0.7.11",
5+
"version": "0.7.12",
66
"main": "dist/jsoneditor.js",
77
"author": {
88
"name": "Jeremy Dorn",

src/core.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ JSONEditor.prototype = {
109109
this.callbacks = this.callbacks || {};
110110
this.callbacks[event] = this.callbacks[event] || [];
111111
this.callbacks[event].push(callback);
112+
113+
return this;
112114
},
113115
off: function(event, callback) {
114116
// Specific callback
@@ -131,13 +133,29 @@ JSONEditor.prototype = {
131133
else {
132134
this.callbacks = {};
133135
}
136+
137+
return this;
134138
},
135139
trigger: function(event) {
136140
if(this.callbacks && this.callbacks[event] && this.callbacks[event].length) {
137141
for(var i=0; i<this.callbacks[event].length; i++) {
138142
this.callbacks[event][i]();
139143
}
140144
}
145+
146+
return this;
147+
},
148+
setOption: function(option, value) {
149+
if(option === "show_errors") {
150+
this.options.show_errors = value;
151+
this.onChange();
152+
}
153+
// Only the `show_errors` option is supported for now
154+
else {
155+
throw "Option "+option+" must be set during instantiation and cannot be changed later";
156+
}
157+
158+
return this;
141159
},
142160
getEditorClass: function(schema) {
143161
var classname;
@@ -180,10 +198,15 @@ JSONEditor.prototype = {
180198
if(self.options.show_errors !== "never") {
181199
self.root.showValidationErrors(self.validation_results);
182200
}
201+
else {
202+
self.root.showValidationErrors([]);
203+
}
183204

184205
// Fire change event
185206
self.trigger('change');
186207
});
208+
209+
return this;
187210
},
188211
compileTemplate: function(template, name) {
189212
name = name || JSONEditor.defaults.template;
@@ -489,10 +512,10 @@ JSONEditor.prototype = {
489512
}, []);
490513
}
491514
// Type should be intersected and is either an array or string
492-
else if(prop === 'type') {
515+
else if(prop === 'type' && (typeof val === "string" || Array.isArray(val))) {
493516
// Make sure we're dealing with arrays
494-
if(typeof val !== "object") val = [val];
495-
if(typeof obj2.type !== "object") obj2.type = [obj2.type];
517+
if(typeof val === "string") val = [val];
518+
if(typeof obj2.type === "string") obj2.type = [obj2.type];
496519

497520

498521
extended.type = val.filter(function(n) {

src/editors/string.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@ JSONEditor.defaults.editors.string = JSONEditor.AbstractEditor.extend({
382382
var self = this;
383383

384384
if(this.jsoneditor.options.show_errors === "always") {}
385-
else if(!this.is_dirty) return;
386-
385+
else if(!this.is_dirty && this.previous_error_setting===this.jsoneditor.options.show_errors) return;
387386

387+
this.previous_error_setting = this.jsoneditor.options.show_errors;
388388

389389
var messages = [];
390390
$each(errors,function(i,error) {

src/intro.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*! JSON Editor v0.7.11 - JSON Schema -> HTML Editor
1+
/*! JSON Editor v0.7.12 - JSON Schema -> HTML Editor
22
* By Jeremy Dorn - https://github.com/jdorn/json-editor/
33
* Released under the MIT license
44
*
5-
* Date: 2014-09-28
5+
* Date: 2014-10-05
66
*/
77

88
/**

0 commit comments

Comments
 (0)