我有一个 JSONEditor npm 包的绑定处理程序。我的程序将使用户能够比较这些面板中的两个不同的 JSON 文件。我的 bindingHandler 看起来像这样:
var editor = undefined;
ko.bindingHandlers.jsoneditor = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
const options = {
"mode" : "view"
};
const value = ko.utils.unwrapObservable(valueAccessor());
if(!editor) {
editor = new JSONEditor(element, options);
editor.set(value);
}
else if(element !== editor.container){
editor = new JSONEditor(element, options);
editor.set(value);
}
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
const options = {
"mode" : "view"
};
const value = ko.utils.unwrapObservable(valueAccessor());
if(element === editor.container){
editor.destroy();
editor = new JSONEditor(element, options);
editor.set(value);
}
else {
//editor.set(value);
editor = new JSONEditor(element, options);
editor.destroy();
}
}
};
这样做的问题是,每当我切换面板、element
更改并使用编辑器创建错误时。
这是HTML:
<div class="jsoneditor body" id="editor-1" data-bind="jsoneditor: subject1"></div>
<div class="jsoneditor body" id="editor-2" data-bind="jsoneditor: subject2"></div>