Javascript 防止CodeMirror json lint删除空输入
我正在为JSON准备一个快速的CodeMirror输入,为了确保用户不会把事情搞砸,我正在使用JSON lint插件。我的问题是,在渲染空的CodeMirror输入时,会立即显示lint错误。我理解空输入并不构成有效的JSON,但我希望它只在输入完成后运行 我使用的是Javascript 防止CodeMirror json lint删除空输入,javascript,codemirror,jsonlint,ui-codemirror,Javascript,Codemirror,Jsonlint,Ui Codemirror,我正在为JSON准备一个快速的CodeMirror输入,为了确保用户不会把事情搞砸,我正在使用JSON lint插件。我的问题是,在渲染空的CodeMirror输入时,会立即显示lint错误。我理解空输入并不构成有效的JSON,但我希望它只在输入完成后运行 我使用的是addon/lint/json lint.jsaddon,它反过来又使用包 JS var jsonConfig = { mode: 'application/json', lineNumbe
addon/lint/json lint.js
addon,它反过来又使用包
JS
var jsonConfig = {
mode: 'application/json',
lineNumbers: true,
lint: true,
autoCloseBrackets: true,
gutters: ['CodeMirror-lint-markers']
};
$('.json textarea').each(function (pos, el) {
CodeMirror.fromTextArea(el, jsonConfig);
});
空输入结果示例:
Lint消息:
我在文档中看不到任何东西可以禁用空输入的linting。我缺少一些简单的东西吗?您可以像这样为文本区域设置一个默认值
{\n\t\n}
{
}
你会有这样的东西
{\n\t\n}
{
}
希望这有助于:
var editor_json = CodeMirror.fromTextArea(document.getElementById("textAreaId"), {
lineNumbers: true,
mode: "application/json",
gutters: ["CodeMirror-lint-markers"],
lint: true,
theme: '<yourThemeName>'
});
//on load check if the textarea is empty and disable validation
editor_json.setOption("lint", editor_json.getValue().trim() );
//once changes happen, if the textarea gets filled up again re-enable the validation
editor_json.on("change", function(cm, change) {
editor_json.setOption("lint", editor_json.getValue().trim() );
});
///sometimes you need to refresh the CodeMirror instance to fix alignment problems or some other glitch
setTimeout(function(){
editor_json.refresh();
},0);
翻译成: