简体   繁体   English

检索可观察值的当前值

[英]Retrieve current value of an observable

If I want to get the value of an observable, I typically call it as a function. 如果我想获得一个可观察值,我通常将其称为函数。

Say, I have a model with observables as below 说,我有一个可观察的模型如下

export interface ClientModel {
    name: KnockoutObservable<string>;
    age: KnockoutObservable<number>;
}

And a model without observable as below 以及以下无法观察到的模型

export interface ServerModel {
   name: string;
   age: number;
}

Below are two implementations of converting from one model to another. 以下是从一种模型转换为另一种模型的两种实现。

A 一种

export function getServerModelJson(person: ClientModel) : ServerModel {
   const personJson: ServerModel = {
      name: ko.unwrap(person.name),
      age: ko.unwrap(person.age)       
   }
   return personJson;
}

B

export function getServerModelJson(person: ClientModel) : ServerModel {
   const personJson: ServerModel = {
      name: person.name(),
      age: perosn.age()       
   }
   return personJson;
}

Is there a difference between the two (calling the observable as function versus using ko.unwrap). 两者之间是否有区别(将observable称为函数与使用ko.unwrap进行调用)。 What is the recommended way? 推荐的方法是什么? What changes if the fields are complex types. 如果字段是复杂类型,将会发生什么变化。 In those cases, how do you do the conversion? 在这种情况下,如何进行转换?

Is there a different between the two (calling the observable as function versus using ko.unwrap ). 两者之间有什么区别(将observable称为function与使用ko.unwrap )。

Two: 二:

  1. If you call the VM property as a function directly, it must be a function (eg, an observable or whatever). 如果直接将VM属性作为一个函数调用,则它必须是一个函数(例如,可观察的对象或诸如此类)。 If it's a simple non-function data property, you'll get an error. 如果这是一个简单的非功能数据属性,则会出现错误。 With ko.unwrap , it gives you the simple non-function data property's value in that case (or, of course, the observable's value). 使用ko.unwrap ,它可以为您提供简单的非功能数据属性在这种情况下的值(或者,当然是可观察值的值)。

  2. ko.unwrap adds a function call and a couple of checks (the call to ko.unwrap and the work it does to determine whether what you've passed it is an obserable). ko.unwrap添加了一个函数调用和一些检查(对ko.unwrap的调用ko.unwrap的工作来确定您通过的内容是否令人讨厌)。

If you know it's observable, as in your example, there's no real reason to use ko.unwap . 如您的示例所示,如果您知道它是可观察的,则没有真正的理由使用ko.unwap (If you know it's not observable, similarly there's no real reason to use ko.unwrap .) (如果您知道它是不可观察的,那么同样没有使用ko.unwrap真正理由。)

What is the recommended way? 推荐的方法是什么?

It's up to you, based on what your properties are and whether you know they're observables. 取决于您的属性是什么,以及是否知道它们是可观察的。

What changes if the fields are complex types. 如果字段是复杂类型,将会发生什么变化。 In those cases, how do you do the conversion? 在这种情况下,如何进行转换?

Nothing, you just do it the same way —note that if the properties of the complex type are also observables, you'll have to handle them separately ( ko.unwrap doesn't go deep). 没什么,您只是以相同的方式进行操作-请注意,如果复杂类型的属性也是可观察的,则必须分别处理它们( ko.unwrap并不深入)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM