Created
July 25, 2011 20:06
-
-
Save svieira/1105052 to your computer and use it in GitHub Desktop.
Knockout: Detatchable dependentObservable
This file contains hidden or 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
// A dependentObservable that can be written to and reverted. | |
ko.detatchableDependentObservable = function(value, model) { | |
var root_value = typeof value === 'function' ? ko.dependentObservable(value, model) : ko.observable(value); | |
var public_value = ko.observable(root_value()); | |
root_value.subscribe(function(value){ | |
public_value(value); | |
}); | |
var public_ = ko.dependentObservable({read: public_value, write: function(value){ public_.isConnected(false); public_value(value); }}); | |
public_.isConnected = ko.observable(true); | |
public_.reconnect = function(){ this.isConnected(true); public_value(root_value()); }; | |
public_.disconnect = function(){ this.isConnected(false); }; | |
return public_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment