Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best place to put Cordova/Phonegap events in AngularJS

I have an AngularJS Cordova application and everything is going really well at the moment. My next step is to add Cordova plugins to the application such as Cordova Connect plugin to check if the network connection is on and listen to network events.

The plan is to listen to these network events and ask the Connect plugin if the device has a connection to the internet, if not I will redirect to an error page.

I'm struggling to find a place in my AngularJS application where to register these events on application startup.

Should they be in the main run block, config block or inside some kind of factory/service/provider?

Where are you guys putting these outside-AngularJS device events?

fx.

document.addEventListener("online", yourCallbackFunction, false);

like image 469
Snorri Örn Daníelsson Avatar asked Jan 07 '14 15:01

Snorri Örn Daníelsson


1 Answers

I have it myModule.run my app.js and works just great, I actually have also other cordova events there.

enter image description here

MyModule.run(function ($rootScope, $http, dataService, $window, $q, $location, localize) {

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

    //Initialize anything you need to. aka: Google analytics.

    //Set other evens you need to listen to.
    document.addEventListener("online", onOnline, false);
    document.addEventListener("offline", onOffline, false);
 }
}

Hope this helps!

like image 154
Arcayne Avatar answered Nov 17 '22 04:11

Arcayne