Skip to content

Instantly share code, notes, and snippets.

@rogden
Created April 28, 2014 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogden/11377139 to your computer and use it in GitHub Desktop.
Save rogden/11377139 to your computer and use it in GitHub Desktop.
Knockout Binding - templateFromChild
ko.bindingHandlers.templateFromChild = {
init: function (element, valueAccessor, allBindings) {
var selector = ko.unwrap(valueAccessor()),
tmpl;
// selector can either be an #ID or the string 'first' which just needs to save the first child as the template
if (selector === 'first') {
tmpl = ko.virtualElements.firstChild(element);
} else {
tmpl = document.getElementById(selector);
}
// empty the container node
ko.virtualElements.emptyNode(element);
// re-append the template only
element.appendChild(tmpl);
}
};
ko.bindingHandlers.foreach.after = ['templateFromChild'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment