Ionic resolve a promise on app start - angularjs

I am running the ionic framework v1 and my app does some initial set up before getting started. Part of this set up requires getting the users location which should be completed before the app calls any other initialization function. Otherwise, it won't work.
So my question is how do I get ionic to call a function - Wait for the promise to resolve. And then begin to initialize the controller, routes, and whatnot.
It looks like the $ionicPlatform.ready function is called before app load but if I put it in there it does not block and it just proceeds to begin to initialize my app before the promise is returned.

Related

How to make angular waiting for load session info data from the server?

I have session info on the server and it uses in many controllers of my app. How can I make an angular wait until session load at startup?
I recommend you to use a loading bar or something like that.
Loading bar: http://chieffancypants.github.io/angular-loading-bar/
Beasides, you may create a angular service (for e.g. SessionService) which returns a promise for Session data from the server.
Finally, inside the angujar's run method do this:
1 - Start the loading bar.
2 - Call your angular session service.
3 - Once the session service promise was resolved stop the loading bar.
Example:
angular.module('myApp', []).
run(function() {
startLoadingBar();
callService.then(function(){
stopLoadingBar();
});
});
I hope it helps.

Cordova resume application and angular $apply() on PhoneGap app

I have Cordova/Ionic/AngularJS application I am currently testing using the PhoneGap app on iOS. This application uses WebSockets to receive external inbound messages. An Angular service function pushes the new messages in an array and binds the array to a scope. I then use $rootScope.$apply() to propagate the change to my view that displays this array of messages.
Everything works fine except when I pause and resume the application. In that case, any new incoming message is indeed correctly stored in the messages array however the view is not updated as I would expect from $apply(). Leaving the view and returning to it fixes the problem. It's as if reloading the view's controller and calling the service resets something.

Wait for app to finish initialization before changing route

When I start my app I have an initialization process that shows some loading indicator and shows the actual content after everything is loaded. Now when I change the Url manually, the initialization process is also triggered, which is good, but the view controller assigned to that route is also created and starts its work right away before the initialization is completed. I tried to use a resolve property at my routes, where I return a promise that is resolved once the app is loaded, however I put this promise in a service and thats forbidden to be injected during the config phase.
How can I wait for the app to be completely loaded before I change the route?

Authentication mechanism with AngularJS (loading the user session)

I am using AngluarJS since 2 days and i am now trying to build my authentication system.
I have several controllers within i need to reflect connected user credentials. Something like:
<body>
<div ng-controller="topController">
<h1>{{User.name}}</h1>
</div>
<div ng-controller="contentController">
<h1>{{User.age}}</h1>
</div>
<div ng-controller="bottomController">
<h1>{{User.email}}</h1>
</div>
</body>
So since it's not appropriate to load the User into a controller, i guess the best way is to work with an external service and get it back on the different controllers's callbacks.
But by doing that, i am wondering how (where) could we load the session at the fisrt loading of the page (let's imagine i just have a single view to ignore the router mechanism) ?
I am looking for a kind of "main" point.
I am using the facebook authentication so my question is not really about how to use the cookies with angular, but more about the "global logic" to set up.
If someone could give me an idea about how to do that, or give me some explanation about a best way to think the authentication mechanism, it would be nice !
You generally do global initialization code by passing an initialization function to the run method of your main app module like so:
angular.module('myApp', [/* some dependencies */])
.run(function(/* some dependencies */) {
// Do anything you want to do after all modules load
// but before your DOM/directives are processed by Angular.
});
See here: http://code.angularjs.org/1.2.6/docs/api/angular.Module#methods_run
This would seem to be a good place to do any session stuff you want to do with Facebook. If your Facebook interactions are asynchronous, then you'll probably need to delay your main app controllers ("topController", "contentController" etc) from doing their thing until your figure out what's happening with the session (since it won't happen synchronously in this run function). One solution to this could be to set up routing so that you don't get routed to the view with those main app controllers until the session logic finishes. In the run initialization function, when your asynchronous FB session stuff is done (and you've stored the session info in a Service or something), you can make a call to change routes so you load the main app view, and at that point whatever Service you're storing the session in should be all set with your FB session.

Angularjs suitable moment to launch a http request when app start

I just started to learn Angularjs 2 days ago. A question have confused me for all the 2 days.
I need to make a http request to the server to get some data when the app start, but I cannot find the suitable moment to do this.
I've tried to make a controller, which calls $http.get(). But It doesn't work. It seems that the controller won't be instantiated if it's never used in the html (not sure about this).
I also tried to find other ways, but I only found $http which is used for http request. And $http only appears in the controller.
Maybe I need use other Angularjs methods? Or, I should do the instantiate action manually?
Something like:
angular.module('yourApp').run(['$rootScope', '$http', function ($rootScope, $http) {
// do stuff here
}]);
From the documentation:
Run Blocks
Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the service have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

Resources