Knockout Update Array

Example for Brenn

by Jason Butz

HTML

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<table>
    <tbody data-bind="foreach: jsObject" id="dataTable">
        <tr>
            <td data-bind="text: value, attr: {'data-index': $index, 'data-id': id}"></td>
        </tr>
    </tbody>
</table>
<button id="update">Update ID 789</button>

JavaScript

function model() {
    var self = this;
    self.jsObject = ko.observableArray([{
        id: '123',
        value: 'a'
    }, {
        id: '456',
        value: 'b'
    }, {
        id: '789',
        value: 'c'
    }]);
}



$('#update').click(function () {
    var index = parseInt($('#dataTable td[data-id="789"]').data('index'));
    var array = pageModel.jsObject();
    array[index].value = 'HOLA';
    console.log(array)
    pageModel.jsObject(array);
    console.log(pageModel.jsObject());
});

var pageModel = new model();
ko.applyBindings(pageModel);