简体   繁体   English

淘汰赛计算与自定义绑定计算顺序

[英]Knockout computed vs custom binding calculation order

I defined a custom binding that alters some way an observable's property, let's say customProperty. 我定义了一个自定义绑定,它以某种方式改变了可观察对象的属性,例如customProperty。 (for example): (例如):

ko.bindingHandlers.customBinding = {
    update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        //computes some value depending on observable value
        //...
        valueAccessor().customProperty = someValue;
    }
};

I also have a computed observable that triggers on my customBinded observable, in which I need the value of customProperty updated. 我还有一个可计算的observable,它在我的customBinded observable上触发,在其中我需要更新customProperty的值。 It turns out logging inside custom binding and in computed code that computed is calculated before customBinding, so it reads old customProperty value. 事实证明,在自定义绑定内部和在customBinding之前计算的已计算代码中记录日志,因此它读取旧的customProperty值。

Can I specify that binding has priority over computeds, or is there some workaround to achieve computed to "wait" for custom binding? 我可以指定绑定优先于计算绑定,还是可以通过某种方法来实现“等待”自定义绑定?

You can use the Rate-limiting observable notifications to delay the execution of the computed observable. 您可以使用“ 速率限制可观察”通知来延迟执行所计算的可观察对象。 eg 例如

viewModel.computed = ko.computed(function() {
  var input = viewModel.input();
  return viewModel.input.customProperty + ' ' + input;
}).extend({rateLimit: 10 });

The slight delay will allow the binding to execute before the computed observable. 轻微的延迟将允许绑定在计算的可观察值之前执行。 See this JSFiddle example for a demonstration. 请参见此JSFiddle示例以进行演示。

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

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