One problem is with:
if(App.isOffline) { // <--- THIS ALWAYS RETURNS TRUE, doh
You should call it as a function, instead of just accessing it. A function is a truthy value, but what you want is the result of the function.
Now, for the rest of the functionality. It makes sense to have encapsulate it on a service and make it available to the rest of your controllers/views. For simplicity, I would make it part of ApplicationController.
App.ApplicationController = Ember.Controller.extend( {
isOnline: true, // assume we're online until proven wrong
init: function () {
updateNetworkStatus();
},
updateNetworkStatus: function () {
var appController = this;
if (!navigator.onLine) {
this.set('isOnline', false);
return; // return early, no point in pinging the server if we have no LAN
}
Ember.$.get('myserver.com/status').done(function () {
// todo: consider checking the result
appController.set('isOnline', true);
}).fail(function () {
appController.set('isOnline', false);
}).always(function () {
Ember.run.later(appController, 'updateNetworkStatus', 60000);
});
}
});
We still check the navigator.online, but if it's true, we don't assume we have a connection all the way to the server. We check that. Once we get the response we update the flag and keep updating that flag every minute.
From other controllers, we can simply add a dependency to the application controller.
App.OtherController = Ember.Controller.extend( {
needs: ['application'],
});
And then use it in properties or as a check before doing certain action:
App.OtherController = Ember.Controller.extend( {
needs: ['application'],
canEdit: function () {
return this.get('controllers.application.isOnline');
}.property('controllers.application.isOnline')
});
Or from your view
{{controllers.application.isOnline}}
Hope this helps. As I said, I would normally encapsulate this into a service
that can then later be injected into controllers just to move this type of concerns away from application, but that probably requires answering a second question.
Hope this helps