Skip to content

Instantly share code, notes, and snippets.

@lukes
Last active March 10, 2017 08:43
Show Gist options
  • Save lukes/e190d5db75204bc1ca64 to your computer and use it in GitHub Desktop.
Save lukes/e190d5db75204bc1ca64 to your computer and use it in GitHub Desktop.
Ember online/offline connection status service
// (Rename to connection-status and add to /initializers)
export default {
name: 'connectionStatus',
initialize: function(container, app) {
app.inject('route', 'connectionStatus', 'service:connectionStatus');
app.inject('controller', 'connectionStatus', 'service:connectionStatus');
}
};
// (Rename to connection-status and add to /services)
/* global window, navigator */
import Ember from "ember";
export default Ember.Object.extend({
online: false,
offline: Ember.computed.not('online'),
startWatching: function() {
this.set('online', navigator.onLine);
var _this = this;
var updateStatus = function() {
_this.set('online', navigator.onLine);
if (_this.get('online')) {
Ember.Logger.info('Connection status: User has gone online');
} else {
Ember.Logger.info('Connection status: User has gone offline');
}
};
window.addEventListener('online', updateStatus);
window.addEventListener('offline', updateStatus);
}.on('init')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment