I'm just getting started with knockout.js, and I've hit a snag. I can bind text variables, but can't seem to do anything with templates. What's going wrong here?
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="underscore-min.js" type="text/javascript"></script>
<script src="knockout-1.2.1.js" type="text/javascript"></script>
<script id="myTemplate" type="text/x-jquery-tmpl">
{{each objs}}
${var1} ${var2}<br/>
{{/each}}
</script>
<script type="text/javascript">
var myObj = function(var1, var2) {
this.var1 = var1;
this.var2 = var2;
}
var viewModel = {
myText: ko.observable("See...?"),
objs: ko.observableArray([
new myObj("Foo","Bar"),
new myObj("Foo","Bar")
])
};
</script>
<script type="text/javascript">
$(document).ready(function(){
ko.applyBindings(viewModel);
});
</script>
</head>
<body>
<div id="main">
This works:
<span data-bind="text: myText"></span>
<br/>
There should be stuff here:
<span data-bind="template: 'myTemplate'"></span>
But there isn't.
</div>
</body>
</html>
BTW, I get this output:
This works: See...?
There should be stuff here: But there isn't.
Aha. I was missing jquery.tmpl.min.js.
One thing about the knockout documentation--it's really heavy on the inline examples, but it's hard to find an example that actually sets up the files.