Skip to content

Instantly share code, notes, and snippets.

@svieira
Created July 25, 2011 20:06
Show Gist options
  • Save svieira/1105052 to your computer and use it in GitHub Desktop.
Save svieira/1105052 to your computer and use it in GitHub Desktop.
Knockout: Detatchable dependentObservable
// 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