I'm trying to write a custom binding to bind knockout to a froala-editor.
My binding works in the following way:
ko.bindingHandlers.froala = {
init: function(element, valueAccessor){
var options = {
inlineMode: false,
alwaysBlank: true,
buttons : ["bold", "italic", "createLink"],
autosaveInterval: 10,
contentChangedCallback: function () {
var html = $(element).editable("getHTML"),
observable = valueAccessor();
observable( html );
}
};
$(element).editable(options);
// handle disposal (if KO removed by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).editable("destroy");
});
},
update: function(element, valueAccessor){
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).editable("setHTML", value);
}
}
Here I am using autosaveInterval, because I was not able to find any more suitable method.
My HTML is really simple:
<div data-bind="froala: txt"></div>
The corresponding JS:
function test() {
this.txt = ko.observable('Hello');
}
var a = new test();
ko.applyBindings(a);
Everything works, but the only problem is that after each autosaveInterval
time, the focus from my editor is lost. After investigating I found that observable( html );
is the culprit. If I comment it out, focus is not lost, but it is understandable that my model is not synchronized.
Can anyone try to help me?
Thanks to @nemesv, I was able to make a jsfiddle with a repro.