Include a jquery.tmpl template for knockout.js?
Silly question, maybe, but I can't find a documented answer开发者_运维知识库 anywhere.
I'm trying to save a jquery tmpl template, and include it as a script. Seems like this should work:
<script src="my_tmpl.js" type="text/javascript" id="myTemplate"></script>
But no luck. What am I doing wrong?
Just for completeness' sake, here's the kind of binding I'm doing for knockout:
<div id="myTemplatedBox" data-bind="template: 'myTemplate'">
Edit: Here's a very reduced version of the my_tmpl.js contents. It works fine when I keep it in the main html document.
<div class="headerText">{{html header_text}}</div>
{{each(i,v) answer_array}}
<div class="questionText"><input type="radio" name="Q${i}" value="${i+1}">{{html v}}</input></div>
{{/each}}
Take a look at http://encosia.com/jquery-templates-composite-rendering-and-remote-loading/
It looks like you still need the script tag in your template file.
<script id="invoiceTemplate" type="x-jquery-tmpl">
<div class="questionBox">
<div class="headerText">{{html header_text}}</div>
{{each(i,v) answer_array}}
<div class="questionText"><input type="radio" name="Q${i}" value="${i+1}">{{html v}}</input></div>
{{/each}}
</div>
</script>
Change the type to text\html and give it another go:
<script src="my_tmpl.js" type="text/html" id="myTemplate"></script>
精彩评论