Open discussion about best way to delay "main" view until ionicPlatform.ready


#1

Hey everyone. I’d like to start a discussion (as I assume there may not be one right answer, although maybe there is) about the best way to solve a problem I’ve run into a few times w/ Ionic apps. Mainly - I want to use some Cordova plugin (device, media, whatever) in my first view, but I can’t because the deviceready event, and by proxy, ionicPlatform.ready, fires after that first view is up and running.

I spoke with someone a few weeks back recently when I first ran into this (and I forget his name, sorry!) who mentioned that my controller could use a service that can return a promise such that it is resolved when deviceready is done and would then be able to do whatever it needs to do.

I worked up another way as well by just using an initial view that’s nothing more than a loading page (http://www.raymondcamden.com/2014/8/16/Ionic-and-Cordovas-DeviceReady--My-Solution).

I just can’t help but think that this should be easier. :slight_smile: Maybe I’m asking for too much, but I’d love it if the boiler plate code could handle this for me - basically - don’t do any UI until deviceready so I can simply not worry about it. I understand that may not be an option, but I just wanted to throw this up here to see what other people think.


#2

Using the side-menu template, I have solved this problem by adding $ionicPlatform.ready() in the side-menu controller and removing completely .run() in app.js.
I have used this solution to fire $cordovaNetwork of ngCrodova.


#3

Would you mind sharing the code of your example?


#4

This is my controller.js

angular.module('starter.controllers', ['ngCordova', 'ionic'])
    
.controller('AppCtrl', function($scope, $rootScope, $ionicPlatform, $cordovaNetwork) {
    	
$ionicPlatform.ready(function() {
        
 $rootScope.isOnline = $cordovaNetwork.isOnline();

	if ($rootScope.isOnline)
         {
           //some code
         }
         else
             {
                //some code
             }
   });
});
 //use result in other controller
.controller('HomeCtrl', function($scope, $rootScope){
      if ($rootScope.isOnline)
         {
           //some code
         }
         else
             {
                //some code
             }
   });

#5

Respectfully, that does not look right to me. I assume cordovaNetwork.isOnline is checking to see if the device is online? If so, that isn’t related at all to the deviceready event, and in fact, would be bad if the app is used in offline mode.


#6

I’m sorry but I had understood that you wanted to fire a function of a Cordova plugin in “deviceready” event (and so also in the main view) that should match with $ionicPlatform.ready
I’m sorry for my poor english.


#7

I’m facing the same problem. I’m using SQLite plugin, and as the doc says, my database has to be initiated on device ready. But, I’m also injecting an object (that came from my SQLite database) in my first view’s controller using resolve. So, when my first view is rendered, the database is not ready yet and my object doesn’t resolve, leading to a routeChangeError.

Well, I think this is just one of problems using $ionicPlatform.ready() and first view that use plugins.

To solve my problem, I’m bootstrapping my angular application manually, after the deviceready event. So, in my app.run, I can execute everything without $ionicPlatform.ready().

See this: http://stackoverflow.com/a/27450072


#8

How come this is solved in another release?

This should be a function itself…


#9

I have the exact same issue, namely using the SQLite plugin that needs to be ready when the view gets loaded. Although ionic is really nice, I cannot believe such a basic requirement is not being thought of, or is there something we are missing and should know?
A basic structure and tutorial would be highly appreciated.
Coming form a zero experience background, the learning curve is quite steep and simple things seem extremely complicated (very easy and straight forward in Titanium Studio for example).

I will try whatever was posted, thanks for sharing.