ko.utils.unwrapObservable 和 ko.toJS 有什么区别?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们之间存在差异。
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.
关于 ko.mapping.toJS(...) 与 ko.toJS(...)
ko.映射.toJS()
因此,这意味着映射插件只会转换回 IT 为您创建的内容。如果您合并了视图模型或添加了计算的甚至只是常规的可观察量,它们将不会返回到模型中。
如果您有混合视图模型(部分映射和部分手动创建),您最好创建 JSON 以混合方式发送回服务器。
您可以将其放在页面上以查看这两个模型有何不同:(
我不确定如何“美化”来自
ko.mapping.toJSON
的响应,所以我只使用了 JSON.stringifyWith respect to
ko.mapping.toJS(...)
vs.ko.toJS(...)
ko.mapping.toJS()
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:
(I wasn't sure how to 'prettify' the response from
ko.mapping.toJSON
so I just used JSON.stringify这就是 ko.mapping.toJS & 的方式ko.toJS:
如您所见,ko.mapping.toJS() 返回一个反映视图模型的干净的 JS 对象,
不留下任何淘汰属性,而 ko.toJS() 留下一些淘汰属性..
我不知道他们为什么这样做......
this is how ko.mapping.toJS & ko.toJS:
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...