Skip to content

Instantly share code, notes, and snippets.

@kevinold
Forked from greyarch/ko-persist.js
Created June 28, 2013 14:46
Show Gist options
  • Save kevinold/5885228 to your computer and use it in GitHub Desktop.
Save kevinold/5885228 to your computer and use it in GitHub Desktop.
ko.extenders.persist = function (target, option) {
target.subscribe(function (newValue) {
window.localStorage.setItem(option, newValue);
});
return target;
};
//and use it like this:
var item = ko.observable().extend({persist:"storedItem"});
//or if you want to also initialize the observable with the stored value:
var item = ko.observable(window.localStorage.getItem("storedItem")).extend({persist:"storedItem"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment