Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Created September 22, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smailliwcs/76edc4c6f66ad3a6fac47e8d731fa1ef to your computer and use it in GitHub Desktop.
Save smailliwcs/76edc4c6f66ad3a6fac47e8d731fa1ef to your computer and use it in GitHub Desktop.
Knockout extender: cache
ko.extenders.cache1 = function (target) {
target.cached = ko.observable(target.peek());
target.cache = function () {
target.cached(target());
};
return target;
};
var x = ko.observable(1).extend({ cache1: {} });
x(2);
console.log(x());
console.log(x.cached());
x.cache();
console.log(x.cached());
ko.extenders.cache2 = function (target) {
var cached = ko.observable(target.peek());
var result = ko.computed({
read: cached,
write: function (value) {
target(value);
}
});
result.current = target;
result.cache = function () {
cached(target());
};
return result;
};
var y = ko.observable(3).extend({ cache2: {} });
y(4);
console.log(y.current());
console.log(y());
y.cache();
console.log(y());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment