Skip to content

Commit

Permalink
Fix bug with object properties named "type". Fixes #183
Browse files Browse the repository at this point in the history
Add setOption method (only works for `show_errors` currently). For #242
Make window.jsoneditor accessible from demo.html to help with debugging.
  • Loading branch information
jdorn committed Oct 6, 2014
1 parent a4c17cf commit 5cb68dd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-editor",
"version": "0.7.11",
"version": "0.7.12",
"authors": [
"Jeremy Dorn <[email protected]>"
],
Expand Down
1 change: 1 addition & 0 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ <h2>Code</h2>
schema: schema,
startval: startval
});
window.jsoneditor = jsoneditor;

// When the value of the editor changes, update the JSON output and validation message
jsoneditor.on('change',function() {
Expand Down
37 changes: 30 additions & 7 deletions dist/jsoneditor.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*! JSON Editor v0.7.11 - JSON Schema -> HTML Editor
/*! JSON Editor v0.7.12 - JSON Schema -> HTML Editor
* By Jeremy Dorn - https://github.com/jdorn/json-editor/
* Released under the MIT license
*
* Date: 2014-09-28
* Date: 2014-10-05
*/

/**
Expand Down Expand Up @@ -307,6 +307,8 @@ JSONEditor.prototype = {
this.callbacks = this.callbacks || {};
this.callbacks[event] = this.callbacks[event] || [];
this.callbacks[event].push(callback);

return this;
},
off: function(event, callback) {
// Specific callback
Expand All @@ -329,13 +331,29 @@ JSONEditor.prototype = {
else {
this.callbacks = {};
}

return this;
},
trigger: function(event) {
if(this.callbacks && this.callbacks[event] && this.callbacks[event].length) {
for(var i=0; i<this.callbacks[event].length; i++) {
this.callbacks[event][i]();
}
}

return this;
},
setOption: function(option, value) {
if(option === "show_errors") {
this.options.show_errors = value;
this.onChange();
}
// Only the `show_errors` option is supported for now
else {
throw "Option "+option+" must be set during instantiation and cannot be changed later";
}

return this;
},
getEditorClass: function(schema) {
var classname;
Expand Down Expand Up @@ -378,10 +396,15 @@ JSONEditor.prototype = {
if(self.options.show_errors !== "never") {
self.root.showValidationErrors(self.validation_results);
}
else {
self.root.showValidationErrors([]);
}

// Fire change event
self.trigger('change');
});

return this;
},
compileTemplate: function(template, name) {
name = name || JSONEditor.defaults.template;
Expand Down Expand Up @@ -687,10 +710,10 @@ JSONEditor.prototype = {
}, []);
}
// Type should be intersected and is either an array or string
else if(prop === 'type') {
else if(prop === 'type' && (typeof val === "string" || Array.isArray(val))) {
// Make sure we're dealing with arrays
if(typeof val !== "object") val = [val];
if(typeof obj2.type !== "object") obj2.type = [obj2.type];
if(typeof val === "string") val = [val];
if(typeof obj2.type === "string") obj2.type = [obj2.type];


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

if(this.jsoneditor.options.show_errors === "always") {}
else if(!this.is_dirty) return;

else if(!this.is_dirty && this.previous_error_setting===this.jsoneditor.options.show_errors) return;

this.previous_error_setting = this.jsoneditor.options.show_errors;

var messages = [];
$each(errors,function(i,error) {
Expand Down
8 changes: 4 additions & 4 deletions dist/jsoneditor.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "json-editor",
"title": "JSONEditor",
"description": "JSON Schema based editor",
"version": "0.7.11",
"version": "0.7.12",
"main": "dist/jsoneditor.js",
"author": {
"name": "Jeremy Dorn",
Expand Down
29 changes: 26 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ JSONEditor.prototype = {
this.callbacks = this.callbacks || {};
this.callbacks[event] = this.callbacks[event] || [];
this.callbacks[event].push(callback);

return this;
},
off: function(event, callback) {
// Specific callback
Expand All @@ -131,13 +133,29 @@ JSONEditor.prototype = {
else {
this.callbacks = {};
}

return this;
},
trigger: function(event) {
if(this.callbacks && this.callbacks[event] && this.callbacks[event].length) {
for(var i=0; i<this.callbacks[event].length; i++) {
this.callbacks[event][i]();
}
}

return this;
},
setOption: function(option, value) {
if(option === "show_errors") {
this.options.show_errors = value;
this.onChange();
}
// Only the `show_errors` option is supported for now
else {
throw "Option "+option+" must be set during instantiation and cannot be changed later";
}

return this;
},
getEditorClass: function(schema) {
var classname;
Expand Down Expand Up @@ -180,10 +198,15 @@ JSONEditor.prototype = {
if(self.options.show_errors !== "never") {
self.root.showValidationErrors(self.validation_results);
}
else {
self.root.showValidationErrors([]);
}

// Fire change event
self.trigger('change');
});

return this;
},
compileTemplate: function(template, name) {
name = name || JSONEditor.defaults.template;
Expand Down Expand Up @@ -489,10 +512,10 @@ JSONEditor.prototype = {
}, []);
}
// Type should be intersected and is either an array or string
else if(prop === 'type') {
else if(prop === 'type' && (typeof val === "string" || Array.isArray(val))) {
// Make sure we're dealing with arrays
if(typeof val !== "object") val = [val];
if(typeof obj2.type !== "object") obj2.type = [obj2.type];
if(typeof val === "string") val = [val];
if(typeof obj2.type === "string") obj2.type = [obj2.type];


extended.type = val.filter(function(n) {
Expand Down
4 changes: 2 additions & 2 deletions src/editors/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ JSONEditor.defaults.editors.string = JSONEditor.AbstractEditor.extend({
var self = this;

if(this.jsoneditor.options.show_errors === "always") {}
else if(!this.is_dirty) return;

else if(!this.is_dirty && this.previous_error_setting===this.jsoneditor.options.show_errors) return;

this.previous_error_setting = this.jsoneditor.options.show_errors;

var messages = [];
$each(errors,function(i,error) {
Expand Down
4 changes: 2 additions & 2 deletions src/intro.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*! JSON Editor v0.7.11 - JSON Schema -> HTML Editor
/*! JSON Editor v0.7.12 - JSON Schema -> HTML Editor
* By Jeremy Dorn - https://github.com/jdorn/json-editor/
* Released under the MIT license
*
* Date: 2014-09-28
* Date: 2014-10-05
*/

/**
Expand Down

0 comments on commit 5cb68dd

Please sign in to comment.