jwysiwyg textarea的knockout绑定处理程序

时间:2012-02-05 00:20:34

标签: javascript html knockout.js jwysiwyg

所以我注意到淘汰js与jQuery jwysiwyg插件不兼容。

阅读此博文后:http://www.knockmeout.net/2011/07/another-look-at-custom-bindings-for.html 这个回答:How to detect a change with jQuery to the contents of a textarea that's using jWYSIWYG plugin?

我编写了以下绑定处理程序:

    ko.bindingHandlers.wysiwyg = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var options = allBindingsAccessor().wysiwygOptions || {};
        var value = ko.utils.unwrapObservable(valueAccessor());
        var $e = $(element);
        $.extend(true, {
            initialContent : value
        }, options);

        $e.wysiwyg(options);

        //handle the field changing
        function detectFn() {
            var observable = valueAccessor();
            var newvalue = $e.wysiwyg("getContent");
            observable(newvalue);
        }

        var current = $e.wysiwyg('document');
        var timer;
        current.bind({    
            keyup: function(){
                clearTimeout(timer);
                timer = setTimeout(detectFn, 1000);
            }
        });

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            $e.wysiwyg('destroy');
        });
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).wysiwyg("setContent", value);
        ko.bindingHandlers.value.update(element, valueAccessor);
    }
};

像这样使用:

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

到目前为止,它对我有用,任何意见都将受到赞赏。

我认为这对于任何想要将kockout js和jwysiwyg一起工作来绑定textarea元素的人都有用。

0 个答案:

没有答案