knockout valueUpdate not working with Pagedown?

隐身守侯 提交于 2019-12-05 22:07:11

This is a genuine use-case for using a custom binding. I implemented TinyMCE against a <textarea> successfully with this method.

The problem you are observing is the manipulations you make by clicking buttons on the tool bar are raising events on the Markdown.Editor which alters the value of the underlying <textarea> without the change event being fired, which of course Knockout relies upon in order to notify it's subscribables.

My solution implements a custom binding to ensure that events raised by the wysiwyg editor are reflected in the view-model. Specifically, to ensure that the value is always up-to-date as well as maintaining a dirty flag in the view-model. Since I am unfamiliar with the Markdown plug-in I have included a sample taken from my TinyMCE solution. The principle will be exactly the same you will just need to apply the specifics of the Markdown editor.

ko.bindingHandlers.wysiwyg = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        $(element).tinymce({
            /*** other options excluded for brevity ***/
            setup: function(editor) {
                editor.on('change', function() {
                    valueAccessor()(editor.getContent());
                    viewModel.isDirty = editor.isDirty();
                });
            }
        });
    },
    update: function(element, valueAccessor) {
            $(element).text(valueAccessor()());
    }
};

Finally your binding can now be implemented as follows;

<textarea data-bind="value: content, wysiwyg: content"></textarea>

UPDATE

Since reading up on PageDown, here is the custom binding taken from the fork of your JSFiddle

ko.bindingHandlers.wysiwyg = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        editor.hooks.chain("onPreviewRefresh", function () {
            $(element).change();
        });
        editor.run();
    }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!