4

In my custom binding, I cannot write data back to model. The problem is that there is no way to write into "writable" property.

In knockout 2 there was possibility to use allBindingsAccessor()._ko_property_writers But in version 3 there are no such thing.

  ko.bindingHandlers.htmlValue = {
    init: function(element, valueAccessor, allBindingsAccessor, context) {
      var handler;
      handler = function() {
        var value, writable;
        writable = valueAccessor();
        console.log(allBindingsAccessor());
        value = element.innerHTML;
        // NOTE In previous versions this could be used:
        //writable(value);
        //writable = value <-- this also does not update model, as writable is simple string
        // Or this:
        //allBindingsAccessor()._ko_property_writers

        return allBindingsAccessor().htmlValue = value;
      };
      ko.utils.registerEventHandler(element, "keyup", handler);
      ko.utils.registerEventHandler(document, "click", handler);
    },
    update: function(element, valueAccessor) {
      var value;
      value = valueAccessor();
      if (element.innerHTML !== value) {
        element.innerHTML = value;
      }
    }
  };

var data = {
    name: 'blah'
};

ko.track(data);
ko.applyBindings({data: data});

My html:

<div contenteditable="true" data-bind="htmlValue: data.name"></div>
<input data-bind="value: data.name, valueUpdate: 'afterkeydown'"></input>
<input data-bind="value: data.name, valueUpdate: 'afterkeydown'"></input>

Js fiddle with this example: http://jsfiddle.net/t5rWd/2/

Expected behaviour: Inputs should update after changing div content (it's contenteditable)

Current behaviour: It works like one-way binding, div get's updated but cannot update inputs.

4

1 回答 1

0

请注意,OP 的评论实际上可能会作为答案。这是knockout-es5-plugin.

鉴于 OP 的帐户现在处于非活动状态,我将发布此社区 wiki 答案以保留该信息。完整评论:

你是对的,这是插件问题,这里是解决这个问题的拉取请求:github.com/SteveSanderson/knockout-es5/pull/15 – user133408 2014-06-26 12:42:07

于 2015-05-10T10:17:40.690 回答