Skip to content

Instantly share code, notes, and snippets.

@christo8989
Last active October 2, 2019 04:30
Show Gist options
  • Save christo8989/6a95a0ad97539128dce3899cd203a119 to your computer and use it in GitHub Desktop.
Save christo8989/6a95a0ad97539128dce3899cd203a119 to your computer and use it in GitHub Desktop.
Custom binding handler that passes the element to the viewmodel. Typically, this should not be used but sometimes its easier to just get the element. In my case, some event happens and I want to focus on my input element.
ko.bindingHandlers.element = {
init: function (element, valueAccessor) {
var vmElement = valueAccessor();
if (ko.isObservable(vmElement) === false)
throw Error("The binding handler needs an observable.");
vmElement(element);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment