Standalone edit doesn't clear field after save

Standalone edit doesn't clear field after save

mguinnessmguinness Posts: 85Questions: 12Answers: 1

When using Standalone model after clearing a field and submitting the data that field retains it's old value (isn't cleared).

This occurs since the JSON returned from the endpoint is serialized to ignore null values. I assume that the editor code only sets properties from the returned data which makes sense. However if the property is omitted from the returned data then the field retains it's old value.

I was able to workaround this by adding the following code to the edit event so that missing fields from the returned JSON are cleared. However it would be ideal if this could be incorporated into the editor library. Thoughts?

$.each(editor.fields(), function (idx, fld) {
    if (!data.hasOwnProperty(fld)) {
        var el = $('[data-editor-field="' + fld + '"]');
        if (el.filter('[data-editor-value]').length)
            el.attr('data-editor-value', '');
        else
            el.empty();
    }
});

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,482Questions: 1Answers: 10,467 Site admin
    Answer ✓

    Hi,

    You are correct - that is basically what is required and is actually intentionally put in like that. The reason being that Editor generally expects the server to return the full data set. If it doesn't, Editor can't know what the intention was (latest update time, empty the element, remove the element, etc) thus it is required to be explicit like you have done.

    Allan

  • mguinnessmguinness Posts: 85Questions: 12Answers: 1

    Fair enough, but for my application I won't be returning the full data set. Another workaround is to use preEdit event which others might find useful. Thanks for your answer.

    .on('preEdit', function (e, json, data, id) {
        $.each(editor.fields(), function (idx, fld) {
            if (!data.hasOwnProperty(fld))
                data[fld] = null;
        });
    })
    
This discussion has been closed.