Skip to content

Instantly share code, notes, and snippets.

@q-state
Forked from jpo/knockout.cleditor.js
Created October 17, 2013 16:23
Show Gist options
  • Save q-state/7027905 to your computer and use it in GitHub Desktop.
Save q-state/7027905 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();
if ($editor[0].doc.body.innerHTML !== value) {
$editor[0].doc.body.innerHTML = value;
$editor[0].focus();
}
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment