淘汰赛:计算出的可观察值与函数 [英] Knockout: computed observable vs function

查看:29
本文介绍了淘汰赛:计算出的可观察值与函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用淘汰赛时,使用只读计算的 observables 而不是简单的函数有什么优势?

When using knockout, what is the advantage of using read-only computed observables rather than simple functions?

以下面的viewmodel构造函数和html代码片段为例:

Take the following viewmodel constructor and html snippet, for example: ​

var ViewModel = function(){
    var self = this;
    self.someProperty = ko.observable("abc");
    self.anotherProperty = ko.observable("xyz");
    self.someComputedProperty = function(){
        return self.someProperty() + self.anotherProperty();
    };    
};

<input data-bind="value: someProperty"/>
<input data-bind="value: anotherProperty"/>
<p data-bind="text: someComputedProperty()"></p>

这里的一切似乎都如您所愿,所以我有理由改为使用:

Everything here seems to work as you'd expect, so is there a reason why I should instead use:

​var ViewModel = function(){
    var self = this;
    self.someProperty = ko.observable("abc");
    self.anotherProperty = ko.observable("xyz");
    self.someComputedProperty = ko.computed(function(){
        return self.someProperty() + self.anotherProperty();
    });    
};


<input data-bind="value: someProperty"/>
<input data-bind="value: anotherProperty"/>
<p data-bind="text: someComputedProperty"></p>

我注意到 http://knockoutjs.com/documentation/computedObservables.html 声明...声明性绑定只是作为计算的 observable 实现的",那么这是否意味着我需要在我的视图模型中显式使用它们?

I notice that the documentation at http://knockoutjs.com/documentation/computedObservables.html states that "...declarative bindings are simply implemented as computed observables", so does that mean there's need for me to use them explicitly in my viewmodels?

推荐答案

如果计算 observable 的唯一目的是对其进行简单的绑定,那么使用函数将是等效的.绑定在计算的 observable 内部实现以跟踪依赖关系,因此当任何 observable 发生变化时,它会重新触发您的绑定.

If the only purpose of your computed observable is to do a simple binding against it, then using a function would be equivalent. Bindings are implemented inside of a computed observable to track the dependencies, so it will re-trigger your binding when any of the observables change.

以下是关于计算的可观察量与函数的一些考虑事项

Here are a few things to consider about computed observables vs. a function

  • 计算出的 observable 的值被缓存,所以它只在它被创建和依赖被更新时更新.对于常规函数,您每次都需要执行逻辑.如果很多事情都依赖于该值(假设集合中的每个项目都绑定到来自父项的值),那么该逻辑将一次又一次地运行.

  • the value of a computed observable is cached, so it is only updated when it is created and when a dependency is updated. For a regular function, you would need to execute the logic each time. If many things depend on that value (say each item in a collection is binding against a value from the parent), then this logic will be getting run again and again.

在您的 JavaScript 中,您还可以像使用其他 observable 一样自由地使用计算 observable.这意味着您可以针对它们创建手动订阅,并从其他计算中依赖它们(调用函数也会创建此依赖项).您可以依靠 KO 中的常规实用程序方法(例如 ko.utils.unwrapObservable)来确定是否需要将其作为函数调用或不需要检索值.

in your JavaScript, you are also free to use computed observables like you would other observables. This means that you can create manual subscriptions against them and depend on them from other computeds (calling a function would also create this dependency). You can rely on the normal utility methods in KO like ko.utils.unwrapObservable to generically determine if you need to call it as a function or not to retrieve the value.

如果最终你想把值传送到服务器,一个计算的 observable 自然会出现在你的 JSON 输出中,而作为普通函数结果的值在转换为 JSON 时就会消失(你会必须先做更多工作才能从该函数填充属性).

if ultimately you want to ship the value to the server, a computed observable will naturally appear in your JSON output, while a value that is the result of a normal function will just disappear when converted to JSON (you would have to do more work to populate a property from that function first).

这篇关于淘汰赛:计算出的可观察值与函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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