rodikh/angular-json-editor

Updating startval has no effect

redwyre opened this issue · 3 comments

There is no watch on startval so changing it doesn't update the editor. I am using bindings to update the json-editor and this resulted in default values in the editor.

Usage:

<div ng-if="vm.selectedPlugin.editableObjectSchema">
    <json-editor schema="vm.selectedPlugin.editableObjectSchema" startval="vm.selectedPlugin.editableObject" buttons-controller="SyncButtonsController" on-change="onChange($editorValue)">
</div>

I hacked the directive to reload on startval change using the following code which seems to work. I had to modify the reset function so only a schema change would re-get the value from the editor, instead of always.

function restart() {
    var values = startVal;
    if (scope.editor && scope.editor.destroy) {
        scope.editor.destroy();
    }

    scope.editor = new JSONEditor(element[0], {
        startval: values,
        schema: schema
    }, true);

    scope.editor.on('ready', editorReady);
    scope.editor.on('change', editorChange);
    element.append(buttons);
}

snip

scope.$watch('schema', function (newVal, oldVal) {
    //update newScheme
    if (newVal.success) {
        newVal.success(function (data) {
            schema = data;
            if (scope.editor) {
                startVal = scope.editor.getValue();
            }
        });
    } else {
        schema = newVal;
        if (scope.editor) {
            startVal = scope.editor.getValue();
        }
    }

    restart();
}, true);

scope.$watch('startval', function (newVal, oldVal) {
    //update newScheme
    if (newVal && newVal.success) {
        newVal.success(function (data) {
            startVal = data;
        });
    } else {
        startVal = newVal;
    }

    restart();
}, true);

Hey redwyre,

What's the sense of changing the Startval? It should only set the initial state of the form.
Otherwise you are risking deleting the user's input by overriding it with default values.

It would make more sense to update the editor using setValue.

Hey rodikh,
I'm trying to use one json-editor, but I have multiple tabs where selecting a tab will change both the schema and the startval via binding.

Perhaps you should have a few editors, one for each tab.

Otherwise, what solution do you propose?
How about setting the new schema and value inside the buttons controller using editor.setValue?