Stack overflow: How can I load data from jsrender template and update i
https://stackoverflow.com/q/74155152/1054484
by BorisMoore
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://www.jsviews.com/download/jsviews.js"></script>
</head>
<body>
<script id="pageTmpl" type="text/x-jsrender">
{^{for items}}
<button id="btn{{:#index}}">Click me JQ</button>
<button data-link="{on ~updateItem}">Click me JsV</button>
{^{if showItem}}
<div>
This is your item.
{{:name}}
</div>
{{else}}
<div>
No item.
{{:name}}
</div>
{{/if}}
{{/for}}
</script>
<div id="page"></div>
</body>
</html>
JavaScript
"use strict";
var pageTmpl = $.templates("#pageTmpl"),
data = {items: [
{showItem:true, name:"Johnny Depp"},
{showItem:false, name:"Henry Best"}
]},
helpers = {
updateItem: function(ev) {
$.observable(this).setProperty("showItem", !this.showItem);
}
};
pageTmpl.link("#page", data, helpers); // Render and link the template
$("#btn0").on("click", function() {
var $tmplItem = $(this).view().ctx.root.items[0];
$.observable($tmplItem).setProperty("showItem", !$tmplItem.showItem);
});
$("#btn1").on("click", function() {
var $tmplItem = $(this).view().ctx.root.items[1];
$.observable($tmplItem).setProperty("showItem", !$tmplItem.showItem);
});