Shouldn't we be manually bootstrapping the app?


#1

I currently have a cordova app that uses a custom framework (which I don’t really like) and would love to switch to angularjs/ionic.

Currently, the app does something like:

$(document).ready(function () {
	if (window.cordova) {
		document.addEventListener('deviceready', __initialize, false);
	} else {
		__initialize();
	}
});

This ensures that the document and the device (if running on a device) is ready before any other code is executed.

I know ionic has the $ionicPlatform.ready function, but to me code that requires the device to be ready may be executed before the device is ready by using this.

For instance, if we take a look at the demo weather app, notice that the WeatherCtrl uses $ionicPlatform.ready to hide the status bar. Also notice that the WeatherCtrl depends on the Geo and Weather services. What would happen if the Geo or Weather services required cordova features (and therefore the device to be ready) before they could function? Wouldn’t this create a race condition?

Wouldn’t it be better if we manually bootstrapped angular so that nothing is executed until the device is ready?

angular.element(document).ready(function() {
	if (window.cordova) {
		document.addEventListener('deviceready', function() {
			angular.bootstrap(document.body, ['app']);
		}, false);
	} else {
		angular.bootstrap(document.body, ['app']);
	}
});

ngCordova not working properly
#2

I did something very similar and found it to be quite useful. Makes for much cleaner code in my opinion as well.


#3

I also think this is a good idea, as I am having a lot of issues with device ready.

This might be a dumb question, but where exactly does the bootstrap code go? Before the app.js code, or in a different file like main.js and then loaded before the other scripts?


#4

I put that in an app-bootstrap.js file and than included that at the bottom of the page (just before the </body> tag).

See here for more information on manually bootstrapping angular/ionic


#5

Awesome, thanks for the quick reply.


#6

I just put my bootstrap logic at the top of app.js