I can't figure out why my code doesn't work. The full code is at the
end. The part I can't figure out is here:
<P data-bind="click: function(){item('I work')}">I work</P>
<div id="item_display" data-bind="template: {name: 'item-display-
tmpl', foreach: input}"></div>
<script id="item-display-tmpl" type="text/html">
<!-- change the 'P' to a 'button' and get "missing ) after argument
list" -->
<P data-bind="click: function(){item('${name}')}">${name}</P>
</script>
<!-- move the following commented line into the script and there's an
error -->
<!--<button data-bind="click: function(){item(${id})}">${name}</
button>-->
In short, I want lines which look like:
<P>Andrew</P>
<P>Dalke</P>
and when I click on the paragraph I get the text (or the id) shown
elsewhere.
The problem is the <P> data-bind works outside of the template but not
in the template. That is, I can click on the "I work" and see it, but
I can't click on the generated "Andrew" to see it.
I'm missing something in my understanding because if I change
<P data-bind="click: function(){item('${name}')}">${name}</P>
-to-
<button data-bind="click: function(){item('${name}')}">${name}</
button>
(or <a>) then I get the error message from Firefox 3.6.11:
missing ) after argument list
And if I replace that line with the HTML comment
<!--<button data-bind="click: function(){item(${id})}">${name}</
button>-->
then I get the error message "invalid property id".
Can I use bindings in a template? What am I doing wrong?
Best regards,
Andrew Dalke
da...@dalkescientific.com
P.S.
Here's my full page as a reproducible
<html>
<head>
<script src="jquery-1.4.4.min.js"></script>
<script src="jquery.tmpl.min.js"></script>
<script src="knockout-1.1.2.js"></script>
</head>
<body>
<P data-bind="click: function(){item('I work')}">I work</P>
<div id="item_display" data-bind="template: {name: 'item-display-
tmpl', foreach: input}"></div>
<script id="item-display-tmpl" type="text/html">
<!-- change the 'P' to a 'button' and get "missing ) after argument
list" -->
<P data-bind="click: function(){item('${name}')}">${name}</P>
</script>
<!-- move the following commented line into the script and there's an
error -->
<!--<button data-bind="click: function(){item(${id})}">${name}</
button>-->
<P>I see: <span data-bind="text: item"></span></P>
<script>
$(function() {
var data = [ {id: "id1", name: "Andrew"},
{id: "id2", name: "Peter"},
];
var viewModel = {
input: ko.observableArray(data),
item: ko.observable()
};
ko.applyBindings(viewModel);
})
</script>
</body>
</html>