I have a knockout observable array of the form:
this.controls = ko.observableArray([
{
name: 'Previous',
action: '$root.previous'
},
{
name: 'Next' ,
action: '$root.next'
},
{
name: 'Save',
action: '$root.save'
}
]);
I would like to do the following in my view:
<div class="controls navigation">
<ul data-bind="foreach: $root.controls">
<li>
<span data-bind="text: name, click: action"></span>
</li>
</ul>
</div>
The view creates three separate span which are essentially buttons. My goal is have the corresponding action in the viewmodel to be called each time a user clicks on a specific span.
However, this does not work.
How might I go about looping through an array and specifying different click binding actions for each item?
I could easily just write out each span individual (in this specific case, since there is only 3 items in the array), but I am curious how I could accomplish this using the array.