I have having a tough time figuring out how to achieve simply bindings in a tree view template:
This is my requirement:
- Bind checkbox of each node to an isVisible property on the node's underlying view model.
- Bind click action of delete button to a method called remove() on the node's underlying view model.
Treeview is bound to an underlying hierarchical datasource. Items in datasource are created and added dynamically like this:
createCategoryLegendModel(viewModel) {
return {
text : viewModel.label,
imageUrl: this.url + viewModel.imagePath,
expanded : true,
viewModel: viewModel,
callme: function () { alert("view model called"); }, // test method
layers : []
};
}
dataSource.add(createCategoryLegendModel(viewModel));
This is the template I have:
<script id="treeview-template" type="text/kendo-ui-template">
<input type="checkbox" data-bind="checked : item.viewModel.isVisible" style = "margin-left:-5px"></input>
<img data-bind="click : callme" title="click to remove" src="@Url.Content("~/images/delete.png")" style ="margin-top:0px; height:16px; width:16px"/> @* or item.viewModel.remove, which ever I can get to work *@
<span> #: item.viewModel.label #</span>
<span data-bind="text : text"> </span> @* why does this not work *@
</script>
Now, your checkbox example, shows how to render, but does not show us how to bind to an underlying view model. So it does not really help me much. Your templates shows me how to render a button, but not how to bind its click to an underlying view model method yet these are things easily done using Knockout. Also, I the way you bind click actions on the delete button in your templates sample, is susceptible to performance problems and breaks down my MVVM strategy.
This is my requirement:
- Bind checkbox of each node to an isVisible property on the node's underlying view model.
- Bind click action of delete button to a method called remove() on the node's underlying view model.
Treeview is bound to an underlying hierarchical datasource. Items in datasource are created and added dynamically like this:
createCategoryLegendModel(viewModel) {
return {
text : viewModel.label,
imageUrl: this.url + viewModel.imagePath,
expanded : true,
viewModel: viewModel,
callme: function () { alert("view model called"); }, // test method
layers : []
};
}
dataSource.add(createCategoryLegendModel(viewModel));
This is the template I have:
<script id="treeview-template" type="text/kendo-ui-template">
<input type="checkbox" data-bind="checked : item.viewModel.isVisible" style = "margin-left:-5px"></input>
<img data-bind="click : callme" title="click to remove" src="@Url.Content("~/images/delete.png")" style ="margin-top:0px; height:16px; width:16px"/> @* or item.viewModel.remove, which ever I can get to work *@
<span> #: item.viewModel.label #</span>
<span data-bind="text : text"> </span> @* why does this not work *@
</script>
Now, your checkbox example, shows how to render, but does not show us how to bind to an underlying view model. So it does not really help me much. Your templates shows me how to render a button, but not how to bind its click to an underlying view model method yet these are things easily done using Knockout. Also, I the way you bind click actions on the delete button in your templates sample, is susceptible to performance problems and breaks down my MVVM strategy.