Last active
November 8, 2017 18:10
-
-
Save amcdnl/e19281647e37882dcc22 to your computer and use it in GitHub Desktop.
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
// Usage: | |
// OfflineListener.init(true); | |
define(['angular', 'core'], function(angular){ | |
// Some concepts from: https://github.com/HubSpot/offline | |
var module = angular.module('utils.offline', []); | |
module.service('OfflineListener', function ($rootScope, $window, $alert) { | |
var offlineAlert; | |
var hookup = function(){ | |
$rootScope.$on('offline', function(){ | |
// $alert is Angular-Strap component | |
offlineAlert = $alert({ | |
title: 'Internet connection unstable', | |
type: 'danger', | |
dissmissable: false, | |
duration: false | |
}); | |
}, true); | |
$rootScope.$on('online', function(){ | |
if(!offlineAlert) return; | |
offlineAlert.hide(); | |
offlineAlert = false; | |
$alert({ | |
title: 'Internet connection re-established', | |
type: 'info', | |
dissmissable: true, | |
duration: 10 | |
}); | |
}, true); | |
}; | |
var init = function(hookupAlert){ | |
$window.addEventListener("offline", function () { | |
$rootScope.$emit('offline'); | |
}, false); | |
$window.addEventListener("online", function () { | |
$rootScope.$emit('online'); | |
}); | |
if(hookupAlert){ | |
hookup(); | |
} | |
}; | |
return { | |
init: init | |
}; | |
}); | |
return module; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! :) Nice to see how you adapted the JS module to a more "Angluar-friendly" approach.