Here I have this code:
<tbody data-bind="foreach: entries">
<tr>
<td><i class="icon-file"></i> <a href="#" data-bind="text: name, click: $parent.goToPath"></a></td>
</tr>
</tbody>
I would like to have something like this (it’s pseudocode):
<tbody data-bind="foreach: entries">
<tr>
<td><i class="{{ if type == 'file' }} icon-file {{/if}}{{else}} icon-folder {{/else}}"></i> <a href="#" data-bind="text: name, click: {{ if type == 'file' }} $parent.showFile {{/if}}{{else}} $parent.goToPath {{/else}}"></a></td>
</tr>
</tbody>
Is it possible to write something like this on KnockoutJS?
One option is to do something like:
Sample here: http://jsfiddle.net/rniemeyer/9DHHh/
Otherwise, you can simplify your view by moving some logic into your view model like:
Then, add methods on your view model to return the appropriate value:
Sample here: http://jsfiddle.net/rniemeyer/9DHHh/1/