Created
April 28, 2014 16:31
-
-
Save rogden/11377139 to your computer and use it in GitHub Desktop.
Knockout Binding - templateFromChild
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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