Skip to content

Instantly share code, notes, and snippets.

@blockloop
Last active December 18, 2015 03:38
Show Gist options
  • Save blockloop/5719110 to your computer and use it in GitHub Desktop.
Save blockloop/5719110 to your computer and use it in GitHub Desktop.
localStorage with angularjs and node webkit
myApp.controller('MyCtrl', ['$scope','storageService',
function($scope, storageService) {
var brain = storageService.getBrain();
$scope.items = [];
$.extend($scope, brain); // load from memory
$scope.addItem = function(item) {
$scope.items.push(item);
$scope.refresh();
};
$scope.refresh = function() {
brain.items = $scope.items;
storageService.save(brain);
};
}
]);
myApp.service('storageService', function () {
var brain = localStorage.brain;
var cache = sessionStorage.brain;
return {
getBrain: function () {
brain = brain || "{}";
return JSON.parse(brain);
},
getCache: function () {
cache = cache || "{}";
return JSON.parse(cache);
},
saveBrain: function (newBrain) {
newBrain = $.extend(this.getBrain(), newBrain);
localStorage.brain = JSON.stringify(newBrain);
},
saveCache: function (newCache) {
newCache = $.extend(this.getCache(), newCache);
sessionStorage.brain = JSON.stringify(newCache);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment