Skip to content

Knockout.Protected

Areson edited this page Apr 25, 2012 · 2 revisions

Provides "transactional" methods for observables, allowing them to prevent updates to subscribers until a commit is done. All changes made to an observable that are uncommitted will not be reflected by subscribers and will not be available via reads of the observable.

var observable = ko.observable("foo").extend({protected: true});
//This change does not take effect...
observable(42);
//...so the value we ready is still "foo"
var value = observable();

observable.commit();

//Now we read "42"
value = observable();

The standard protection extension provides transactional support for only basic observables and observable arrays. Any observables contained inside the protected observable will not be protected by default. You can force them to be protected by turning on "deep" protection.

var observable = ko.observable({
     foo: ko.observable("foo"),
     bar: ko.observable("bar")
}).extend({protected: {deep: true}});

//These changes won't take effect...
observable().foo(42);
//...until they are committed
observable.commit();

Finally, any uncommitted changes can be rolled back by calling the reset method.

A more complex example can be seen here: http://jsfiddle.net/Areson/D6g3E/

Clone this wiki locally