Skip to content

Instantly share code, notes, and snippets.

@thelinuxlich
Created July 31, 2012 17:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thelinuxlich/3218947 to your computer and use it in GitHub Desktop.
Save thelinuxlich/3218947 to your computer and use it in GitHub Desktop.
Knockout subscribeChanged
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