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

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

问题描述

使用剔除法时,使用只读的可计算观察值而不是简单函数有什么优势?

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指出"...声明式绑定只是作为计算的可观察对象实现的",这是否意味着我需要在我的视图模型中显式使用它们?

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的唯一目的是对其进行简单绑定,则使用函数将是等效的.绑定是在计算的可观察对象内部实现的,以跟踪依赖关系,因此当任何可观察对象发生更改时,它将重新触发您的绑定.

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

  • 已计算的可观察值的值被缓存,因此仅在创建它和更新依赖项时才对其进行更新.对于常规功能,您每次都需要执行逻辑.如果很多事情都依赖于该值(例如,集合中的每个项目都与父项的值绑定),那么该逻辑将一次又一次地运行.

  • 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中,您也可以像其他可观察对象一样自由使用计算可观察对象.这意味着您可以针对它们创建手动订阅,并从其他计算对象中依赖它们(调用函数也将创建此依赖项).您可以依靠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.

如果最终要将该值发送到服务器,则计算得出的可观察值自然会出现在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天全站免登陆