Skip to content

Instantly share code, notes, and snippets.

@mt3o
Created November 8, 2013 21:06
Show Gist options
  • Save mt3o/7377691 to your computer and use it in GitHub Desktop.
Save mt3o/7377691 to your computer and use it in GitHub Desktop.
Sends event when online status is changed and switches between $http and localstorage
angular.module('myApp').run(function($rootScope) {
window.addEventListener("online", function () {
$rootScope.$broadcast('onlineChanged', true);
}, true);
window.addEventListener("offline", function () {
$rootScope.$broadcast('onlineChanged', false);
}, true);
});
angular.module('myApp').factory('storage', function($rootScope, $http, LocalStorage) {
var currentStorage;
$rootScope.$on('onlineChanged', function(evt, isOnline) {
currentStorage = isOnline ? $http : LocalStorage
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment