gpt4 book ai didi

knockout.js - 强制更新受限制的计算属性

转载 作者:行者123 更新时间:2023-12-04 15:22:34 24 4
gpt4 key购买 nike

我有一个表单,其文本输入绑定(bind)到计算属性(包含时间)。用户输入一个值后,它被解析为一个包含总分钟数的整数值。

由于这不是一个微不足道的函数(格式化时间的方法有很多),所以该属性有一个 Throttle Extender .

这一切都很好,问题是当用户输入一个值并立即点击保存按钮时,显然尚未评估限制值。

self.totalMinutes = ko.observable(0);
self.totalMinutesValue = ko.computed({
read: function() {
return MinutesToFormat(self.totalMinutes());
}
write: function(value) {
self.totalMinutes(FormatToMinutes(value));
}
}).extend({ throttle: 250 });

self.Save = function() {
// Send self.totalMinutes() to server, need to ensure the throttled
// computed has been written.
}

有没有一种简单的方法可以强制属性立即从 send 方法更新?我可以添加 setTimeout但这当然远非理想。

最佳答案

看来 rateLimiter 实际上可以解决这个问题。

http://knockoutjs.com/documentation/rateLimit-observable.html

"Writes to observables are not delayed; the observable’s value is updated right away. For writable computed observables, this means that the write function is always run right away.

All change notifications are delayed, including when calling valueHasMutated manually. This means you can’t use valueHasMutated to force a rate-limited observable to notify an un-changed value.

The default rate-limit method is different from the throttle algorithm. To match the throttle behavior, use the notifyWhenChangesStop method."

Evaluation of a rate-limited computed observable isn’t rate-limited; it will re-evaluate if you read its value.

关于knockout.js - 强制更新受限制的计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com