实例化时ko.computed不触发功能 [英] ko.computed do not fire function upon instantiating

查看:155
本文介绍了实例化时ko.computed不触发功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种在实例化ko.computed时不触发该功能的方法

Hi is there a way to not fire the function when instantiating a ko.computed

示例是

我有这个ko.computing

i have this ko.computed

ko.computed(function(){ alert(this.Test); } , this);

所以基本上,如果我实例化此计算,这将触发在那里定义的函数 有没有办法在实例化时不触发它?并且仅在依赖项更改时才触发它?

so basically if i instantiated this computed this will fire the function defined there is there a way not to fire it upon instantiation? and only fire it when dependency change?

推荐答案

您需要设置 deferEvaluation选项:

You need to set the deferEvaluation option:

deferEvaluation-可选.如果此选项为true,则在某些实际尝试访问其值或手动订阅该值之前,将不会计算已计算的observable的值.默认情况下,计算的可观察对象的值会在创建过程中立即确定.

deferEvaluation — Optional. If this option is true, then the value of the computed observable will not be evaluated until something actually attempts to access its value or manually subscribes to it. By default, a computed observable has its value determined immediately during creation.

ko.computed(function(){ alert(this.Test); } , this, { deferEvaluation: true });

这篇关于实例化时ko.computed不触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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