用淘汰赛更新tinymce编辑器

时间:2014-05-29 12:45:29

标签: knockout.js tinymce tinymce-4

我可以按照此处列出的示例获取knockoutjs来创建tinymce编辑器:Binding knockout with tinymce

问题是当我尝试更新editableAreas observableArray中的基础observable时。但是,视图模型更新后,这些更改不会反映在UI中。这是一个jsfiddle显示我正在尝试做的事情: http://jsfiddle.net/rsparkyc/DQ93k/2/

这是javascript:

/*tinymce-knockout-binding v1.0.2|(c) 2014 Michael Papworth|https://raw.github.com/michaelpapworth/tinymce-knockout-binding/master/LICENSE*/
!function(a,b){b.bindingHandlers.wysiwyg={extensions:{},init:function(c,d,e,f,g){if(!b.isWriteableObservable(d()))throw"valueAccessor must be writeable and observable";var h=e.has("wysiwygConfig")?e.get("wysiwygConfig"):{},i=e.has("wysiwygExtensions")?e.get("wysiwygExtensions"):[],j={browser_spellcheck:a(c).prop("spellcheck"),plugins:["link","paste"],toolbar:"undo redo | bold italic | bullist numlist | link",menubar:!1,statusbar:!1,setup:function(a){a.on("change keyup nodechange",function(c){d()(a.getContent());for(var f in i)b.bindingHandlers.wysiwyg.extensions[i[f]](a,c,e,g)})}};j=a.extend(j,h),a(c).text(d()()).tinymce(j),b.utils.domNodeDisposal.addDisposeCallback(c,function(){a(c).tinymce().remove()})},update:function(a,c,d,e,f){return b.bindingHandlers.value.update(a,c,d,e,f)}}}(jQuery,ko),function(a){a.extensions.dirty=function(a,b,c,d){if(c.has("wysiwygDirty")){var e=c.get("wysiwygDirty");if(!ko.isWriteableObservable(e))throw"wysiwygDirty must be writeable and observable";e(a.isDirty())}else d.$root.isDirty=a.isDirty()}}(ko.bindingHandlers.wysiwyg),function(a){a.extensions.wordcount=function(a,b,c,d){var e=a.plugins.wordcount;if(e&&c.has("wysiwygWordCount")){var f=c.get("wysiwygWordCount");if(!ko.isWriteableObservable(f))throw"wysiwygWordCount must be writeable and observable";f(e.getCount())}}}(ko.bindingHandlers.wysiwyg);
//# sourceMappingURL=wysiwyg.min.map


function ViewModel() {
    var self = this;
    self.timesChanged = 0;
    self.editableAreas = ko.observableArray([{
        id : 1,
        txt: ko.observable('first text area')
    },{
        id : 2,
        txt: ko.observable('second text area')
    },{
        id : 3,
        txt: ko.observable('all observable text area')
    }]);
    self.wysiwygOptions = {    
        schema: 'html5',
        inline: true,
        toolbar: 'bold italic underscore',
        menubar: false
    };

    self.changeText = function() {
        self.timesChanged++;
        self.editableAreas()[0].txt('<p>this text has changed ' + self.timesChanged + ' times</p>');   
    }
}

ko.applyBindings(new ViewModel());

这是HTML:

In the paragraph below you can edit your html. Knockout with textarea bindings

<div data-bind="foreach: editableAreas">
    <div class="editableArea" data-bind="wysiwyg: txt, wysiwygConfig: $parent.wysiwygOptions "></div>
</div>
<div>
    <h3>Non-editable area with same data</h3>
    <div data-bind="foreach: editableAreas">
        <div data-bind="text: txt"></div>
    </div>
</div>
<button data-bind="click:changeText">Change Text!</button>

2 个答案:

答案 0 :(得分:2)

首先,我要感谢您决定使用我的绑定。我注意到你使用的是1.0.2版本,此问题已经解决。我可以建议您将绑定更新为latest version (v1.1.1)吗?

答案 1 :(得分:0)

看来wysiwyg插件没有订阅可观察的更改事件。另一种方法是刷新observableArray ......

不幸的是,淘汰赛&#34;价值已经突破&#34;功能对于强制UI更新似乎不可靠。这里记录了一个肮脏的解决方法:

Refresh observableArray when items are not observables

基本上修复是在您更改为observables之后添加此行...

var data = self.array();self.array(null);self.array(data);