Skip to content

Instantly share code, notes, and snippets.

@codescribler
Created August 29, 2012 10:30
Show Gist options
  • Save codescribler/3510096 to your computer and use it in GitHub Desktop.
Save codescribler/3510096 to your computer and use it in GitHub Desktop.
A binding handler for binding knockout observables to a wysihtml5 editor
ko.bindingHandlers.wysihtml5 = {
control: "",
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
control = $(element).wysihtml5({
"events": {
"change" : function() {
var observable = valueAccessor();
observable(control.getValue());
}
}
}).data("wysihtml5").editor;
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var content = valueAccessor();
if (content != undefined) {
control.setValue(content());
}
}
};
@rix0rrr
Copy link

rix0rrr commented Nov 19, 2012

This code is actually wrong once you have more than 1 WYSIHTML5 control on the page, or a variable called "control" in the global scope.

The "control" variable will be shared between all instances of the binding, and the wrong updates will be fired!

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