Search code examples
knockout.jsknockout-mapping-plugin

Knockout.js mapping plugin throws error when using "observe" option


I get

Uncaught TypeError: object is not a function

When trying to do this:

var objToMap = {
    a: 1,
    b: [
        {
            c: {
                d: 1
            }
        }
    ]
};
var mapped = ko.mapping.fromJS(objToMap, { observe: ["a"] })

http://jsfiddle.net/22Bjj/

The docs say that I can use "observe" alone, without specifying "copy".

This is a simplified example, in my real app I have lots of object to copy and only one to observe so using only "observe" is the best for me.


Solution

  • You are using a quite old version of the KO mapping plugin: 2.3.5 while the latest official version is v2.4.1

    But this a latest released version is also containing the bug what you are experiencing:

    'observe' mapping options doesn't work with sub-object

    This was fixed but not released yet so you need to use the absolute latest version of the plugin directly from github.