Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created August 20, 2012 11:42
Show Gist options
  • Save benfoxall/3403443 to your computer and use it in GitHub Desktop.
Save benfoxall/3403443 to your computer and use it in GitHub Desktop.
KO local persistence
var persistLocal = function(obj, storeKey, attrs){
var fromLocal = $.parseJSON(localStorage.getItem(storeKey)||'{}');
$.each(attrs.split(' '), function(i, attr){
obj[attr](fromLocal[attr]);
obj[attr].subscribe(function(v){
fromLocal[attr] = v;
localStorage.setItem(storeKey, JSON.stringify(fromLocal));
});
});
};
function User(){
this.name = ko.observable();
this.email = ko.observable();
this.postcode = ko.observable();
this.password = ko.observable();
this.confirm_password = ko.observable();
persistLocal(this, 'user', 'name email postcode');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment