knockout.js Knockout JS与tinymce文本区域的问题

nkoocmlb  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(150)

Javascript语言

var tiny_options = {
    height: 120,
    width: 300,
    mode: 'textareas',
    theme: 'advanced',
    theme_advanced_buttons1: 'bold,italic,underline',
    theme_advanced_buttons2: '',
    theme_advanced_fonts: 'Arial=arial,helvetica,sans-serif,Courier New=courier new,courier,monospace,Georgia=georgia,times new roman,times,serif,Tahoma=tahoma,arial,helvetica,sans-serif,Times=times new roman,times,serif,Verdana=verdana,arial,helvetica,sans-serif',
    theme_advanced_toolbar_location: 'top',
    theme_advanced_toolbar_align: 'left'
};

//tinymce.init(tiny_options); // Please, remove comment to activate the tinymce

var initData = function (d) {
    this.id = ko.observable(d.id);
    this.text = ko.observable(d.text);
};

var viewModel = function () {
    var self = this,
        data = [{
            id: 1,
            text: 'some text 1'
        }, {
            id: 2,
            text: 'some text 2'
        }];

    self.dataSet = ko.observableArray([]);

    $.each(data, function (i, d) {
        self.dataSet.push(new initData(d));
    });
};

var model = new viewModel();
ko.applyBindings(model);

用户界面

<!-- ko foreach : dataSet -->
<br>
<textarea data-bind="value: text, valueUpdate : 'change'"></textarea>
<br>
<!-- /ko -->

我的天啊
上面的代码运行良好,即模型数据在没有tinymce绑定的情况下可以很好地更新,但是当我激活tinymce时,视图模型可观察性没有更新。我也尝试了**this**,但是没有结果。
那么,请帮助我配置,我如何使用tinymce绑定更新视图模型可观察对象?

rhfm7lfc

rhfm7lfc1#

看起来你需要一个custom binding来绑定这个值,并将TinyMCE编辑器应用到你的<textarea>

<textarea data-bind="wysiwyg: text"></textarea>

给予我在Github上创建的https://github.com/michaelpapworth/tinymce-knockout-binding

b09cbbtk

b09cbbtk2#

这是一个简单的自定义绑定,用于更新可观察对象:

ko.bindingHandlers.richTextEditor = {
    init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
        window.tinymce.init({
            target: element,
            skin: "lightgray",
            menubar: false,
            statusbar: false,
            forced_root_block: false,
            browser_spellcheck: true,
            toolbar: "bold italic underline",
            valid_elements: "strong,br,em,span[style|class|id|data],i[class]",
            formats: {
                bold: { inline: "strong" },
                italic: { inline: "em" },
                underline: { inline: "span", styles: { "text-decoration": "underline" } }
            },
            plugins: "paste",
            resize: false,
            setup: function (editor) {
                editor.on("change", function () {
                    var textInputBinding = allBindings().textInput;
                    var content = this.getContent();
                    textInputBinding && textInputBinding(content);
               });
            }
        });
    }
};

textarea上的绑定应为data-bind="textInput: yourObservable"

相关问题