I am developing a Ionic2 App, using the cordova-plugin-network-information, I am subscribing to the connect and disconnect events from my app.ts and want to be able to pass a reference to my NavController and a Loading component into the subscribe() callback, so whenever the event for a disconnect fires, I can present the user with a Loading overlay on top of the UI. I see that in the callback the reference to the “this” object changes to an object called “SafeSubscriber”, which I think is the rxjs typed class for its Observer object, the problem I have here is that I have no way to get those instances available in app.ts to this code inside the callback, using the Chrome DevTools I also wasn’t able to find my way out of this context in order to access the App object itself.
Here is my code (@ app.ts):
ngOnInit() { // Nav Controller: this.nav = <NavController>this.app.getComponent('nav'); // Disconnect Detection let disconnectSubscription = Network.onDisconnect().subscribe(() => { console.log('Disconnect Detected'); // Push Loading into nav stack here...! // this.nav.present(this.loading); <- no 'this' object... }); }
I have tried setting a “that” object before doing the subscription, so that the variable “this” doesn’t interfere with the callback “this” scope, it didn’t work in this scenario.I know that this is not the best place to put code that changes directly the UI, but I see no other place, since what I need here is a global event handler that works by setting this overlay whenever there is no connection detected and the user is viewing certain pages within the app.I think there should be a very easy and elegant solution to this, but I don’t seem to be able to find it. Is there a way to pass a parameter to the subscribe() function? some sort of object with the references I need?
Thanks in advance.