ko.utils.unwrapObservable 和 ko.toJS 有什么区别?

发布于 2025-01-05 15:26:25 字数 179 浏览 0 评论 0原文

2者有什么区别?

当然,它们是同一件事,因为展开的可观察对象被剥离为 js 原语。那么为什么要有 ko.toJS 或反之亦然呢?

另外,为什么会有 ko.mapping.toJS?这是否与 ko.toJS 没有有效地做同样的事情? knockoutJS 中似乎有几个函数做同样的事情,但它们的存在一定有一个原因。

What are the differences between the 2?

Surely they are the same thing since an unwrapped observable strips down to a js primitive. So why have ko.toJS or vice-versa?

Also, why is there a ko.mapping.toJS? Does this not effectively do the same as ko.toJS?
There seems several functions in knockoutJS which do the same thing but there must be a reason why they exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫嚣ゝ 2025-01-12 15:26:25

它们之间存在差异。

ko.toJS 接受一个对象并且“打开”并清除所有可观察到的对象。它对整个对象图执行此操作。我的行为很像序列化器,这意味着如果您有循环引用,您会遇到问题。它在内部使用 mapJsObjectGraph ,这太复杂了让我现在就可以在脑海中完全编译。但是,当我需要将内容发送回服务器时,我倾向于使用它。

ko.utils.unwrapObservable 只是确定该值是否是可观察的,如果是,则返回基础值。如果不是,它只返回值。如果您使用映射插件,那么它会很方便,例如您最终可能会得到可以同时具有可观察值和不可观察值的模型。

ko.mapping.toJS 是唯一的列表中的一个可能被怀疑在功能上有些重复。它用于将映射的对象图映射回其原始状态。它看起来比 ko.toJS 复杂得多,但我还没有使用过它,所以老实说我无法告诉你更多关于它的信息。在此处了解更多相关信息。

There are differences between them.

The ko.toJS takes an object and "opens up" and cleans the object from all observables. It does this for the entire object graph. I behaves much like a serializer which means that you'll run into problems if you have circular references for instance. It uses mapJsObjectGraph internally which is way too complex for me to fully compile in my head right now. However, I tend to use it when I need to post stuff back to the server.

The ko.utils.unwrapObservable simply determines if the value is an observable and if it is, it returns the underlying value. If not it just returns the value. It can be handy if you you are using the mapping plugin for example where you might end up with models that can have both observables and non observables.

The ko.mapping.toJS is the only one on the list that could be suspected to be somewhat duplicated in terms of its functionality. It is used to map a mapped object graph back to its original state. It looks a lot less complex than ko.toJS but I haven't used it (yet) so I honestly can't tell you more about that one. Read more about it here.

可爱暴击 2025-01-12 15:26:25

关于 ko.mapping.toJS(...) 与 ko.toJS(...)

ko.映射.toJS()

这将创建一个未映射的对象,仅包含以下属性
作为原始 JS 对象一部分的映射对象。所以在
换句话说,您手动添加的任何属性或函数
您的视图模型将被忽略。默认情况下,唯一的例外是
Rule 是 _destroy 属性,它也将被映射回来,因为
这是当你销毁物品时Knockout可能生成的属性
来自 ko.observableArray。有关更多信息,请参阅“高级用法”部分
有关如何配置它的详细信息。

因此,这意味着映射插件只会转换回 IT 为您创建的内容。如果您合并了视图模型或添加了计算的甚至只是常规的可观察量,它们将不会返回到模型中。

如果您有混合视图模型(部分映射和部分手动创建),您最好创建 JSON 以混合方式发送回服务器。

您可以将其放在页面上以查看这两个模型有何不同:(

<h2>ko.TOJSON()</h2>
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>

<h2>ko.mapping.toJS</h2>
<pre data-bind="text: JSON.stringify(ko.mapping.toJS($root), null, ' ')"></pre>

我不确定如何“美化”来自 ko.mapping.toJSON 的响应,所以我只使用了 JSON.stringify

With respect to ko.mapping.toJS(...) vs. ko.toJS(...)

ko.mapping.toJS()

This will create an unmapped object containing only the properties of
the mapped object that were part of your original JS object. So in
other words, any properties or functions that you manually added to
your view model are ignored. By default, the only exception to this
rule is the _destroy property which will also be mapped back, because
it is a property that Knockout may generate when you destroy an item
from an ko.observableArray. See the “Advanced Usage” section for more
details on how to configure this.

So this means the mapping plugin only converts back things that IT created for you. If you've merged a view model or added computed or even just regular observables they won't make it back to the model.

If you have a hybrid viewmodel (partly mapped and partly manually created) you may be best creating the JSON to send back to the server in a hybrid way.

You can put this on your page to see how the two models differ:

<h2>ko.TOJSON()</h2>
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>

<h2>ko.mapping.toJS</h2>
<pre data-bind="text: JSON.stringify(ko.mapping.toJS($root), null, ' ')"></pre>

(I wasn't sure how to 'prettify' the response from ko.mapping.toJSON so I just used JSON.stringify

云雾 2025-01-12 15:26:25

这就是 ko.mapping.toJS & 的方式ko.toJS:

var viewModelRaw = { prop: 0 };
var viewModel = ko.mapping.fromJS(viewModelRaw);
var _raw = ko.mapping.toJS(viewModel); // = { prop: 0 }
var _rawWithTraces = ko.toJS(viewModel);  // = { prop: 0, __ko_mapping__: .... }

如您所见,ko.mapping.toJS() 返回一个反映视图模型的干净的 JS 对象,
不留下任何淘汰属性,而 ko.toJS() 留下一些淘汰属性..

我不知道他们为什么这样做......

this is how ko.mapping.toJS & ko.toJS:

var viewModelRaw = { prop: 0 };
var viewModel = ko.mapping.fromJS(viewModelRaw);
var _raw = ko.mapping.toJS(viewModel); // = { prop: 0 }
var _rawWithTraces = ko.toJS(viewModel);  // = { prop: 0, __ko_mapping__: .... }

As you can see, ko.mapping.toJS() returns a clean JS object reflecting the view model,
Without leaving any knockout properties, while ko.toJS() leaves some knockout properties..

I'm not sure why they did it that way...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文