Get previous value of an observable in subscribe of same observable

Czy możliwe jest w knockout uzyskanie bieżącej wartości obserwowalnej w ramach subskrypcji do tej obserwowalnej, zanim otrzyma nową wartość?

Przykład:

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
});
Author: KodeKreachor, 2012-10-10

5 answers

Istnieje sposób, aby zrobić subskrypcję do wartości before, jak to:

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");
 83
Author: RP Niemeyer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-10 16:24:27
ko.subscribable.fn.subscribeChanged = function (callback) {
    var oldValue;
    this.subscribe(function (_oldValue) {
        oldValue = _oldValue;
    }, this, 'beforeChange');

    this.subscribe(function (newValue) {
        callback(newValue, oldValue);
    });
};

Użyj powyższego w ten sposób:

MyViewModel.MyObservableProperty.subscribeChanged(function (newValue, oldValue) {

});
 141
Author: JBeagle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-12 09:49:54

Mała zmiana do Beagle90 odpowiedz Zawsze zwracaj samą subskrypcję, aby mieć dostęp np. do metody 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;
};
 20
Author: Andries,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-22 09:35:01

Pull request aby dodać tę funkcję ma inny kod, który kończy się lepiej niż poleganie na użyciu zdarzenia beforeChange.

Wszystkie kredyty dla rozwiązania 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);
    });
};

Cytuję Michał:

Początkowo sugerowałem użycie beforeChange do rozwiązania tego problemu, ale od tego czasu zdałem sobie sprawę ,że nie zawsze jest to wiarygodne(na przykład, jeśli wywołasz {[3] } na obserwowalnym).

 15
Author: James Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-05 23:55:30

Odkryłem, że mogę wywołać peek () z zapisywalnego obliczanego obserwowalnego, aby uzyskać wartość przed.

Coś takiego (Zobacz 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);
    }
});
 3
Author: rjmunro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-12 11:16:53