Created
September 22, 2020 15:24
-
-
Save smailliwcs/76edc4c6f66ad3a6fac47e8d731fa1ef to your computer and use it in GitHub Desktop.
Knockout extender: cache
This file contains 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
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()); |
This file contains 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
2 | |
1 | |
2 | |
4 | |
3 | |
4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment