JSFiddle - React, Tailwind, and code Playground

by kougiland

HTML

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"></script>
<h3>
    <span>Selected: </span>
    <span data-bind="text: selectedSection().name" />
</h3>
<div class="tabbable">
    <ul class="nav nav-tabs" data-bind="foreach: sections">
        <li data-bind="css: { active: isSelected }">
            <a href="#" data-bind="click: $parent.selectedSection">
                <span data-bind="text: name" />
            </a>
        </li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" data-bind="template: { name: getTemplate }"></div>
    </div>
</div>

<script id="one" type="text/html">
    <div>One content</div>
</script>

<script id="two" type="text/html">
    <div>Two content</div>
</script>

<script id="three" type="text/html">
    <div>Three contentjnjnjnjnjjnjnjnjn</div>
</script>

JavaScript

var Section = function (name, selected) {
    this.name = name;
    this.isSelected = ko.computed(function() {
       return this === selected();  
    }, this);
}

var ViewModel = function () {
    var self = this;
    
    self.selectedSection = ko.observable();
    
    self.sections = ko.observableArray([
        new Section('one', self.selectedSection),
        new Section('two', self.selectedSection),
        new Section('three', self.selectedSection)
    ]);

    //inialize to the first section
    self.selectedSection(self.sections()[0]);
    
    self.getTemplate = function(item) {
        var selected = self.selectedSection();
        return selected && selected.name;  
    };
}

ko.applyBindings(new ViewModel());