javascript - Chicken or Egg with Resolve, Device Ready and Retrieving Data using SQLite Plugin -
in angular/ionic app, trying redirect user state setup
when opens app first time, , has not setup default account settings yet. this,
- i use database sqlite (from brodysoft in phonegap build consumed ngcordova)
- this database contain table
variables
if , if the user has setup account - using ui-router, attach resolve function first state (
auth
), see below - the resolve function
setupresolve()
tries retrieve data tablevariables
, upon success resolves state 'auth', upon failure redirects user statesetup
here comes issue:
- to open sqlite database , perform queries (such select), device needs ready. in ionic, corresponds wrapping opening of database , queries inside function
$ionicplatform.ready(){}
- see tutorial here. - however, device not ready until resolve functions resolved.
- but again, in order resolve, platform needs ready such sqlite can reached, can happen when functions resolved.
so see in chicken or egg situation. concretely, in code below, function ionicplatform.ready() not firing when opening app on device.
the strange thing works fine on google chrome, guess ionicplatform.ready() bypassed in browser, or there other criteria.
// // i-1 self.opendb = function(method) { // var qopen = $q.defer(); $ionicplatform.ready(function() { // ** never reached when used in resolve context ** // open db if(window.cordova) { db = $cordovasqlite.opendb("starter.db"); } else { db = window.opendatabase("starter.db", "1.0", "my app", 1024 * 1024 * 100); } // create tables self.createtables().then(function(){ qopen.resolve(true); }, function(error){ qopen.reject(error); }) }) return qopen.promise; };
state auth, code in .config
note: setupresolve function calls function opendb()
.state('auth', { url: '/auth', templateurl: 'templates/single-auth.html', controller: 'authctrl', resolve: { setupresolve: setupresolve } })
Comments
Post a Comment