Skip to content

Instantly share code, notes, and snippets.

@jpo
Created May 8, 2013 14:37
Show Gist options
  • Save jpo/5540893 to your computer and use it in GitHub Desktop.
Save jpo/5540893 to your computer and use it in GitHub Desktop.
(function () {
if (typeof (ko) === undefined) {
throw 'Knockout is required, please ensure it is loaded before loading this validation plug-in';
}
ko.bindingHandlers.cleditor = {
init: function(element, valueAccessor, allBindingsAccessor) {
var modelValue = valueAccessor(),
allBindings = allBindingsAccessor();
var $editor = $(element).cleditor({
height: 200,
controls: "bold italic underline | bullets numbering | undo redo"
});
$editor[0].change(function() {
var elementValue = $editor[0].doc.body.innerHTML;
if (ko.isWriteableObservable(modelValue)) {
modelValue(elementValue);
}
else {
if (allBindings['_ko_property_writers'] && allBindings['_ko_property_writers'].cleditor) {
allBindings['_ko_property_writers'].cleditor(elementValue);
}
}
});
},
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()) || '',
$editor = $(element).cleditor();
$editor[0].doc.body.innerHTML = value;
}
};
})();
@q-state
Copy link

q-state commented Oct 17, 2013

Perfect, thank you very much.

@lionelfung
Copy link

could not write data back to value because _ko_property_writers = undefined. Why?

@lionelfung
Copy link

I found this bindingHandler does not support knockout 3.0+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment