I have upgraded knockout and jquery version to 3.5.1 but getting below issue
Uncaught SyntaxError: Unable to process binding "template: function(){return {name:"sectionTemplate",foreach:sections,templateOptions:{ deleteSection:deleteSection,openAddDocumentsDialog:openAddDocumentsDialog}} }" Message: Unexpected token ')'
Is there any issue with knockout library when we use nested template.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://knockoutjs.com/downloads/jquery.tmpl.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-latest.js"></script>
</head>
<body>
<div data-bind="template: { name: 'items', data: breakfast, templateOptions: { header: 'Breakfast Items', message: 'Wake up to these delicious items!', class: 'breakfast' } }"></div>
<script id="items" type="text/html">
<h3>${$item.header}</h3>
<div class="${$item.class}">
{{each $data}}
<p>
<span class="name">${name}</span>
<span class="price">${price}</span>
</p>
{{/each}}
</div>
<p><em>${$item.message}</em></p>
<hr />
<div data-bind="template: { name: 'items2', data: newItems, templateOptions: { header: 'New Items', message: 'Wake up to these delicious items!', class: 'breakfast'} }"></div>
</script>
<script id="items2" type="text/html">
<h3>${$item.header}</h3>
<div class="${$item.class}">
{{each $data}}
<p>
<span class="name">${name}</span>
<span class="price">${price}</span>
</p>
{{/each}}
</div>
<p><em>${$item.message}</em></p>
<hr />
</script>
<script type="text/javascript">
var viewModel = {
breakfast: [
{ name: "toast", price: 2.50 },
{ name: "pancakes", price: 4.00 },
{ name: "eggs", price: 3.25 }],
newItems: [
{ name: "Oats", price: 2.50 }],
};
ko.applyBindings(viewModel);
</script>
</body>
</html>
Specially error is getting when I use nested template item2
Please help me out, Thanks in advance
There seems to be an issue with using
templateOptionsinside a template. Removing that from the item template makes it work. To get around that I would suggest making a view model for the template with the properties that need to be passed in rather than using templateOptions.Also noticed that to access the newItems property, you need to pass in the
$rootto the first template binding rather than thebreakfastproperty.I know its not a fix, but hopefully helps with your debugging.
Important
Also a note about using jquery.tmpl from the knockout documentation - Note 6 -
Just using knockoutjs and removing jquery.tmpl
The following code has removed the need for jquery.tmpl and just using knockoutjs syntax as an alternative way of solving the problem.