Created
November 8, 2013 21:06
-
-
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
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
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