0
votes

I am using the following knockout observableArray and computed column code but the UI (for the data-bound computed) is not updating when I push values to the observable array. Please see my jsFiddle example.

http://jsfiddle.net/U9Sqb/2/

Is there a better way to push values to the observableArray and have them update the UI via a computed? Thanks in advance.

HTML:

<html>
    <head>
        <script src="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-2.1.0.js" ></script>
    </head>
    <body>
        <div data-bind="text: onString"></div>
        <div id="test"></div>
    </body>
</html>

JavaSript:

function MyData() {
  var self = this;
  self.currentOnOf = ko.observable(1);
  self.available = ko.observableArray();
  self.onString = ko.computed(function () {
    return "On " + self.currentOnOf() + " of " + self.available ().length;}, self);
}

var data = new MyData();

$(document).ready(function () {
    ko.applyBindings(data);
    data.available().push(1);
    data.available().push(2);
    $("#test").html(data.available().length);
});
1

1 Answers

5
votes

When you push to the ko.observable array you need to push directly to it:

data.available.push(1);
data.available.push(2);