0

我正在尝试使用 ko.mapping 插件创建函数将计算的 observable 添加到分层视图模型中。但该函数仅适用于顶级“子”数组!代码如下:

Children:[{
    Name: "Page 1",
    Children: [ {
        Name: "Page 1 > 1",
        Children: [{
        Name: "Page 1 > 1 > 1",
        Children: []
    }
,{
        Name: "Page 1 > 1 > 2",
        Children: []
    }
]
    }
    ]
},
{
    Name: "Page 2",
    Children: [
    {
        Name: "Page 16",
        Children: []
    }
    ]
},
{
    Name: "Page 3",
    Children: [
    {
        Name: "aaaaa",
        Children: []
    }
]

我的功能如下

     var computed = function (data) {
                                var self = this;
                                ko.mapping.fromJS(data, {}, self);
                                self.Uri = ko.computed(function () {
                                    console.log(self.Name());
                                    return self.Name() + " Something";
                                });
                            };

     var mapping = {
                    'Children': {
                        create: function (o) {
                            return new computed(o.data);

                        }
                    }
                };

ko.mapping.fromJS(data, mapping, vm);

有没有办法让“计算”函数在所有“子”数组上运行?谢谢。

4

1 回答 1

0

您也应该为孩子提供映射选项。尝试更新computed功能如下:

 var computed = function (data) {
                            var self = this;
                            ko.mapping.fromJS(data, mapping, self);
                            self.Uri = ko.computed(function () {
                                console.log(self.Name());
                                return self.Name() + " Something";
                            });
                        };
于 2013-04-24T14:38:35.130 回答