敲除取消映射后,toJSON视图模型重写未反映 [英] toJSON view model overrides not being reflected after Knockout unmapping

查看:99
本文介绍了敲除取消映射后,toJSON视图模型重写未反映的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器获得了一个深层的对象图,该图由Knockout视图模型结构表示,该结构在每个级别上都省略了各种不必要的属性,但保留了基本层次结构.

I have a deep object graph from the server which I am representing with a Knockout view model structure which omits various unnecessary properties at each level but preserves the basic hierarchy.

我正在双向使用Knockout映射插件.在根之下的每个视图模型级别,当前视图模型的构造函数"都接受其父视图模型作为参考,以及用于构造自身的原始数据的一部分作为参考.然后,它将创建一组新的映射选项以创建自己的子代.

I'm using the Knockout mapping plugin in both directions. At each view model level below the root, the "constructor" of the current view model accepts its parent view model to keep as a reference, plus the part of the original data necessary to construct itself with. It then creates a new set of mapping options to create its own children.

我创建了一个经过简化的小提琴,以说明这一点:

I've created a heavily simplified fiddle which illustrates this:

http://jsfiddle.net/tqPtG/17/

在检索时,我需要从服务器获取大量繁重的只读元数据,但是当我将图作为JSON回发时,应将其省略.我一直在每个级别上使用toJSON原型方法来剔除我不需要的东西,这在某一点上表现出色,但现在已停止工作.

There is a lot of heavy readonly metadata which I need from the server on retrieval, but should be omitted when I post the graph back as JSON. I've been using the toJSON prototype method at each level to cull what I don't need, and this was working beautifully at one point but has now stopped working.

在本地看来,我的toJSON覆盖甚至都没有触发(基于警报和控制台日志),但是在这种提琴中,它们确实触发了,但是最终对象仍然包含已删除的属性"child".我可以看到它已被toJSON方法删除,但是在根目录解除映射后,它仍然存在并显示在警报中.

Locally, it appears that my toJSON overrides do not even fire (based on alerts and console logs), but in this fiddle, they do fire, but the final object still contains the deleted property "child". I can see it being removed by the toJSON method but after the root unmapping it is still there and shown in the alert.

所以,有两个问题-是什么导致我的toJSON覆盖无法在本地触发,我对简化小提琴中的取消映射逻辑的误解在哪里?

So, 2 questions - what would cause my toJSON overrides to not fire locally, and where is my misunderstanding of the unmapping logic in the simplified fiddle?

非常感谢.

推荐答案

这里发生了几件事:

  • 您的toJSON函数将被调用,但不作为ko.mapping.toJSON调用的一部分.映射插件会进行字符串化,作为确定对象键的一部分.
  • ko.mapping.toJSON创建对象的计划副本,此时,它不再是ParentViewModel的实例.然后,当它对对象进行字符串化处理时,将没有理由运行您的toJSON函数.
  • your toJSON function is getting called, but not as part of the ko.mapping.toJSON call. The mapping plugin does a stringify as part of determining a key for objects.
  • ko.mapping.toJSON creates a plan copy of the object and at that point it is no longer an instance of ParentViewModel. Then, when it stringifies the object, it will have no reason to run your toJSON function.

ko.mapping.toJSON的调用确实支持选项的第二个参数.您可以将{ ignore: ['child'] }作为该参数传递,例如: http://jsfiddle.net/rniemeyer/4kHC2/

The call to ko.mapping.toJSON does support a second argument for options. You could pass { ignore: ['child'] } in as this argument like: http://jsfiddle.net/rniemeyer/4kHC2/

这篇关于敲除取消映射后,toJSON视图模型重写未反映的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆