使用属性地址和ko映射忽略选项

时间:2016-07-04 09:52:20

标签: javascript knockout.js knockout-mapping-plugin

虽然只是在docs中提及(搜索" Drilling"的页面),我在impression下面,您可以使用属性地址来忽略某些属性执行KO映射操作时。即。

var mapped = ko.mapping.fromJS(
  {
    ignoreThis: "This will not be ignored",
    complex: {
      includeThis: "This will be included",
      ignoreThis: "This will be ignored"
    }
  }, {ignore: ["complex.ignoreThis"]);

将生成具有以下结构的mapped对象:

{
  ignoreThis: "This will not be ignored",
  complex: {
    includeThis: "This will be included",
  }
}

这可以按预期工作,并且会复制正确的可观察对象。请参阅我的fiddle的映射部分。

然而,映射回来似乎不允许我这样做。例如:

var mapped = ko.mapping.fromJS(
  {
    ignoreThis: "This will not be ignored",
    complex: {
      includeThis: "This will be included",
      ignoreThis: "This will be ignored"
    }
  });

var unmapped = ko.mapping.toJS(mapped, { ignore: ["complex.ignoreThis"]}

在这样的unmapped对象中生成结果:

{
  ignoreThis: "This will not be ignored",
  complex: {
    includeThis: "This will be included",
    ignoreThis: "This will be ignored"
  }
}

(参见我fiddle中的JSON1)

使用此:

var unmapped = ko.mapping.toJS(mapped, { ignore: ["ignoreThis"]}

导致像这样的不受欢迎的unmapped对象......

{
  complex: {
    includeThis: "This will be included",
  }
}

(参见我fiddle中的JSON2)

完全没用......

我做错了什么?

0 个答案:

没有答案