KnockoutJS具有计算可观察量的概念,它是依赖于一个或多个可观察值的函数.淘汰赛能够到
determine the dependencies of a computed observable as described in the docs:
Whenever you declare a computed observable,KO immediately invokes its
evaluator function to get its initial value. While your evaluator
function is running,KO keeps a log of any observables (or computed
observables) that your evaluator reads the value of.
现在,我不明白的是,如果您的计算的observable包含条件逻辑,它是如何工作的.如果Knockout调用评估器函数,肯定有条件逻辑可能导致可观察的函数,该函数取决于不被调用?
我创建了这个小提琴来测试:
var viewmodel = function(first,last) { this.firstName = ko.observable(first); this.lastName = ko.observable(last); this.condition = ko.observable(false); // at the point of evaluation of this computed observabled,'condition' // will be false,yet the dependecy to both firstName and lastName is // identified this.fullName = ko.computed(function() { return this.condition() ? this.firstName() : this.lastName(); },this); };
但是,不知何故Knockout正确地识别了对firstName和lastName的依赖.
有人可以解释一下吗
解决方法
原文地址:https://www.jb51.cc/js/151895.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。