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.