Created
July 31, 2012 17:58
-
-
Save thelinuxlich/3218947 to your computer and use it in GitHub Desktop.
Knockout subscribeChanged
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.subscribable.fn.subscribeChanged = function(callback) { | |
if (!this.previousValueSubscription) { | |
this.previousValueSubscription = this.subscribe(function(_previousValue) { | |
this.previousValue = _previousValue; | |
}, this, 'beforeChange'); | |
} | |
return this.subscribe(function(latestValue) { | |
callback(latestValue, this.previousValue); | |
}, this); | |
}; | |
ko.observableArray.fn.subscribeChanged = function(callback) { | |
if (!this.previousValueSubscription) { | |
this.previousValueSubscription = this.subscribe(function(_previousValue) { | |
this.previousValue = _previousValue.slice(0); | |
}, this, 'beforeChange'); | |
} | |
return this.subscribe(function(latestValue) { | |
callback(latestValue, this.previousValue); | |
}, this); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment