What is the best way to apply a binding to a text fragment?
I came ip with the following code:
var textFragment = '<div><p data-bind="text: text"></p></div>',
htmlTemplate = ko.utils.parseHtmlFragment(textFragment);
ko.applyBindings({
text: "text"
}, htmlTemplate[0]);
alert(htmlTemplate[0].innerHTML);
document.getElementById("test").innerHTML = htmlTemplate[0].innerHTML;
(Fiddler)
which creates a dom node and applys abinding to it and then I insert it to the page. I looked throw the knockout code and saw, that there are several functions for rendering templates and I'm wondering if those methods would work better. I tried to used them but the resulting code was longer and didn't work as expected.
Regards, Stefan
(Not: this is only a simple demo to demonstrate my problem, I'm using it in a custom binding handler).