Last active
December 18, 2015 03:38
-
-
Save blockloop/5719110 to your computer and use it in GitHub Desktop.
localStorage with angularjs and node webkit
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
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); | |
}; | |
} | |
]); | |
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
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