Create a gist now

Instantly share code, notes, and snippets.

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());
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