【发布时间】:2016-09-30 16:06:31
【问题描述】:
我有一个包含多个淘汰组件的应用程序。淘汰组件都绑定到一组可订阅,这些订阅会在单击按钮时更新。每个组件都有几个订阅函数来监听订阅者的变化以调用其中包含的更新函数。
如何将它们配置为仅在更新多个订阅者时调用其更新函数一次?
这是我的大多数组件如何加载的示例 sn-p:
this.OBSERVABLE_NAME.subscribe(function () { this.updateChart(); }, this);
this.OBSERVABLE_NAME.subscribe(function () { this.updateChart(); }, this);
this.OBSERVABLE_NAME.subscribe(function () { this.updateChart(); }, this);
this.OBSERVABLE_NAME.subscribe(function () { this.updateChart(); }, this);
this.updateChart = function () {
$.ajax({
type: "POST",
traditional: true,
url: this.ajax_location,
data: this.ajax_parameters,
success: function (data) {
// DO SOMETHING
}.bind(this),
error: function (returndata) {
alert("Error:\n" + returndata.responseText);
}
});
};
但是,如果我将订阅函数替换为:
ko.computed(function () {
$.ajax({
type: "POST",
traditional: true,
url: this.ajax_location,
data: this.ajax_parameters,
success: function (data) {
// DO SOMETHING
}.bind(this),
error: function (returndata) {
alert("Error:\n" + returndata.responseText);
}
});
}, this).extend({ throttle: 1 });
它似乎按要求工作。我知道油门已贬值,但我很好奇为什么 deferred 似乎无法正常工作。
【问题讨论】:
-
你有一些示例代码吗?这听起来像是延迟更新的一个可能用例,但如果没有代码 sn-p 或示例就很难说。
-
我已经用代码示例更新了问题。