Commit 4ae691e8 by cahrens

Convert JSON that doesn't parse to String (unless object, array, or single quote).

parent f56729db
...@@ -62,16 +62,38 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -62,16 +62,38 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
}, },
onBlur: function (mirror) { onBlur: function (mirror) {
var key = $(mirror.getWrapperElement()).closest('.row').children('.key').attr('id'); var key = $(mirror.getWrapperElement()).closest('.row').children('.key').attr('id');
var quotedValue = mirror.getValue(); var stringValue = $.trim(mirror.getValue());
// update CodeMirror to show the trimmed value.
mirror.setValue(stringValue);
var JSONValue = undefined;
try { try {
var JSONValue = JSON.parse(quotedValue); JSONValue = JSON.parse(stringValue);
self.model.set(key, JSONValue, {validate: true}); } catch (e) {
// If it didn't parse, try converting non-arrays/non-objects to a String.
// But don't convert single-quote strings, which are most likely errors.
var firstNonWhite = stringValue.substring(0, 1);
if (firstNonWhite !== "{" && firstNonWhite !== "[" && firstNonWhite !== "'") {
try {
stringValue = '"'+stringValue +'"';
JSONValue = JSON.parse(stringValue);
mirror.setValue(stringValue);
} catch(quotedE) {
// TODO: validation error
console.log("Error with JSON, even after converting to String.")
console.log(quotedE);
JSONValue = undefined;
} }
catch (e) { }
// TODO: error checking else {
// TODO: validation error
console.log("Error with JSON, but will not convert to String.")
console.log(e); console.log(e);
} }
} }
if (JSONValue !== undefined) {
self.model.set(key, JSONValue, {validate: true});
}
}
}); });
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment