Search code examples
javascriptknockout.jsknockout-mapping-plugin

KnockoutJS observable not updating


Hi I am new to knockoutjs and started learning from the Tutorial in their site.

Observable arrays are supposed to update all the bindings in a page. I am practicing in JsFiddle and my bindings are not being updated dynamically.

What am I doing wrong?

JSFiddle Example here

I am expecting that if we type in the textboxes it should update the table!

JS Code:

var viewmodel = {
    posts: ko.observableArray(data)
};

Solution

  • The problem is that calling posts: ko.observableArray(data) will make only data observable, and not its elements. To make an entire object tree observable, you can use the mapping plugin, like in this updated fiddle: http://jsfiddle.net/gyW2k/7/

    The only change to the code is:

    var viewmodel = {
        posts: ko.mapping.fromJS(data)
    };
    

    ko mapping docs: http://knockoutjs.com/documentation/plugins-mapping.html