当淘汰赛更新值更改事件不触发(Change event doesn't trigger wh

2019-07-30 01:50发布

我有一个外部JavaScript库触发一个textarea的变化,将其格式化等。

然而,当KnockoutJS将值设置为textarea的,将不会触发更改事件。 简化我的问题的小提琴 。 是否有可能火的时候,淘汰赛更新我的textarea的值更改事件?

Answer 1:

而不是试图迫使淘汰赛与您可以设置在底层观察到订阅改变事件的工作。 就像这样: http://jsfiddle.net/EZC9E/1/

this.text.subscribe(function(newValue) {
    alert('Text is changing to ' + newValue);
});        


Answer 2:

除了更改JavaScript库,你可能会考虑它包装成一个自定义淘汰赛绑定 。 您可能已经需要使用您的图书馆反正一个自定义的结合,特别是如果你使用任何foreach生成/动态破坏元素绑定。

一旦你自定义绑定,你有一个非常方便的地方,从触发“改变”。

ko.bindingHandlers.jQueryPluginFunctionCall = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        // Apply jQuery plugin to textarea
        $(element).jQueryPluginFunctionCall();

        // Subscribe to the value binding of the textarea, and trigger the change event.
        allBindingsAccessor().value.subscribe(function () {
            $(element).trigger('change');
        });

        // Clean up jQuery plugin on dispose
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            // This will be called when the element is removed by Knockout or
            // if some other part of your code calls ko.removeNode(element)
            $(element).jQueryPluginFunctionCall('destroy');
        });
    }
}


文章来源: Change event doesn't trigger when Knockout updates value