Skip to content

Instantly share code, notes, and snippets.

@gonzaloruizdevilla
Created September 25, 2012 08:03
Show Gist options
  • Save gonzaloruizdevilla/3780545 to your computer and use it in GitHub Desktop.
Save gonzaloruizdevilla/3780545 to your computer and use it in GitHub Desktop.
AngularJs y detección de conexión para cambiar sistema de almacenamiento
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) {
if( isOnline ){
synchronizeLocalStorageWithServer();
currentStorage = $http;
} else {
currentStorage = LocalStorage;
}
});
});​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment