JSFiddle - React, Tailwind, and code Playground

by Aaron Morgan

HTML

<script src="http://knockoutjs.com/js/jquery-1.4.2.min.js"></script>
<script src="http://knockoutjs.com/downloads/knockout-3.1.0.js"></script>
<button data-bind="click:LoadDevices">Click to Load</button>
<br>
<table id="#devices-table">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: devices">
        <tr>
            <td data-bind="text: id"></td>
            <td data-bind="text: name"></td>
        </tr>
    </tbody>
</table>

JavaScript

var data = {
    "result": true,
        "devices": [{
        "id": "1",
            "name": "first"
    }, {
        "id": "2",
            "name": "last"
    }]
};

Device = function (data) {
    this.id = ko.observable(data.id);
    this.name = ko.observable(data.name);
};

ko.applyBindings({
    devices: ko.observableArray([]),

    LoadDevices: function () {
        var mappedDevices = $.map(data.devices, function (item) {
            return new Device(item);
        });

        this.devices(mappedDevices);
        $('#devices-table').trigger('update');
    }
});