I am building a knockout\jQuery plugin that creates it's own UI when invoked, like so:
<input type="text" name="filter" id="filter" />
// renders ui
$("#filter").myPlugin();
Usual jQuery stuff. However, as part of the behavior I wish to make use of knockout templates. It seems though that knockout only recognises templates that are in the page when it first loads.
Here's a concise example that shows the problem:
<div id="container">
<div data-bind="template: { name: 'fieldsTemplate', foreach: FilterRows() }">
</div>
</div>
And the js:
var fieldstemplate = $('<script>', { type: "text/html", id:"fieldsTemplate" });
var row = $('<ol>').html("<li>row</li>").appendTo(fieldstemplate);
fieldstemplate.insertAfter($("#container"));
var model = { FilterRows: ko.observable(["row"]) };
ko.applyBindings(model);
This produces the following error:
Uncaught TypeError: Cannot read property 'length' of null knockout-2.2.1.js:8
I've also created a JS fiddle of the problem here: http://jsfiddle.net/roysvork/EcFRc/
Bascially I'm wondering whether I need to take another approach here, or if there is a way I can 'parse' the dom to refresh knockouts knowledge of the templates?
Thanks in advance!