在获得新值之前,敲除可能会在可观察到的订阅内获取可观察值的当前值吗?在订阅相同的可观察数据时获取以前的可观察价值
例子:
this.myObservable = ko.observable();
this.myObservable.subscribe(function(newValue){
//I'd like to get the previous value of 'myObservable' here before it's set to newValue
});
在获得新值之前,敲除可能会在可观察到的订阅内获取可观察值的当前值吗?在订阅相同的可观察数据时获取以前的可观察价值
例子:
this.myObservable = ko.observable();
this.myObservable.subscribe(function(newValue){
//I'd like to get the previous value of 'myObservable' here before it's set to newValue
});
有一个办法这样值之前做了订阅的:
this.myObservable = ko.observable();
this.myObservable.subscribe(function(previousValue){
//I'd like to get the previous value of 'myObservable' here before it's set to newValue
}, this, "beforeChange");
我发现,我可以打电话从计算观察到一个可写的PEEK()获得之前的价值。
像这样的东西(见http://jsfiddle.net/4MUWp):
var enclosedObservable = ko.observable();
this.myObservable = ko.computed({
read: enclosedObservable,
write: function (newValue) {
var oldValue = enclosedObservable.peek();
alert(oldValue);
enclosedObservable(newValue);
}
});
ko.subscribable.fn.subscribeChanged = function (callback) {
var oldValue;
this.subscribe(function (_oldValue) {
oldValue = _oldValue;
}, this, 'beforeChange');
this.subscribe(function (newValue) {
callback(newValue, oldValue);
});
};
使用上述这样的:
MyViewModel.MyObservableProperty.subscribeChanged(function (newValue, oldValue) {
});
变化不大Beagle90答案。总是返回订阅本身以便能够访问dispose()。
ko.subscribable.fn.subscribeChanged = function (callback) {
var oldValue;
this.subscribe(function (_oldValue) {
oldValue = _oldValue;
}, this, 'beforeChange');
var subscription = this.subscribe(function (newValue) {
callback(newValue, oldValue);
});
// always return subscription
return subscription;
};
的pull request添加此功能有一些不同的代码,卷起比依靠使用beforeChange
事件更好。
所有信用为解决Michael Best
ko.subscribable.fn.subscribeChanged = function (callback) {
var savedValue = this.peek();
return this.subscribe(function (latestValue) {
var oldValue = savedValue;
savedValue = latestValue;
callback(latestValue, oldValue);
});
};
引述迈克尔:
我使用
beforeChange
来解决这个问题,但都因为意识到它并不总是可靠的(例如最初的建议,如果你在可观察的情况下调用valueHasMutated()
)。
不幸的是,由于在调用订阅回调的时候,值已经改变了,所以'peek()'会给你新的值。 –
@MichaelTeper我知道我在一年前发布了我的答案,但是在我收到了一些提示后,我刚刚测试了它,它确实有效。请参阅:http://jsfiddle.net/4MUWp/ – rjmunro
好吧,我看到你在那里做了什么...问题是关于检索'订阅'回调中的值,这是peek()无法完成的。你的榜样证明什么都没有,并可能混淆一个新来者。你基本上是在这里封装一个私有变量,并在设置它之前显示它的值 - 所以它当然不会改变。 –