Knockout.js mapping options being ignored on nested values

南楼画角 提交于 2019-12-24 01:38:06

问题


I am unable to get the knockout.js mapping options to work on nested values. With the following code, the first level "shouldBeCopied" works fine and the nested "shouldBeCopied" value is always an observable.

var data = {
    shouldBeCopied: "copied",
    nested: {
        shouldBeCopied : "copied"
    }
};

var vm = ko.mapping.fromJS(data, {
    'copy': ["shouldBeCopied"],
    "nested": {
        'copy': ["shouldBeCopied"]
    }
});

console.log(vm);

Any ideas?

Here's a fiddle if anyone wants to play around with it.


回答1:


When using the the copy, ignore, observe, etc options you don't need to minic the object structure in your mapping options (like when using create)

What you need to to is to use a property accessor expression ("nested.shouldBeCopied") in your copy array to configure nested properties:

var data = {
    shouldBeCopied: "copied",
    nested: {
        shouldBeCopied : "copied"
    }
};

var vm = ko.mapping.fromJS(data, {
    'copy': ["shouldBeCopied","nested.shouldBeCopied"]
});

Will output:

Demo JSFiddle.



来源:https://stackoverflow.com/questions/18553708/knockout-js-mapping-options-being-ignored-on-nested-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!