KnockoutJS if statement inside foreach loop

http://stackoverflow.com/questions/9177814/knockoutjs-if-statement-inside-foreach-loop

by rniemeyer

HTML

<script src="https://github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.debug.js"></script>
<table>
<tbody data-bind="foreach: entries">
    <tr>
        <td>
            <i data-bind="attr: { 'class': $parent.getClass($data) }"></i>
            <a href="#" data-bind="text: name, click: $parent.getHandler($data)"></a>
        </td>
    </tr>
</tbody>
</table>

JavaScript

var ViewModel = function() {
    var self = this;
    this.entries = [
        { name: "one", type: 'file' },
        { name: "two", type: 'folder' },
        { name: "three", type: 'file'}
    ];
    
    this.getHandler = function(entry) {
        console.log(entry);
        return entry.type === 'file' ? self.showFile : self.goToPath;
    };
    
    this.getClass = function(entry) {
        return entry.type === 'file' ? 'icon-file' : 'icon-filder'; 
    };
    
    this.showFile = function(file) {
        alert("show file: " + file.name);
    };
    
    this.goToPath = function(path) {
        alert("go to path: " + path.name);
    };
};

ko.applyBindings(new ViewModel());