angularjs - Load data SQLite in ionic bootstrap -
i'm looking solution load data sqlite in cordova bootstrap, more in $ionicplatform.ready until here don't have solution. see piece of code (i'm using ionic $cordovasqlite):
app.js file
... app.run(function($db, $log, params...){ $ionicplatform.ready(function() { ... $db.init().then(function(res){ // here need load user $rootscope.user = res; $log.debug('1 step = user loaded'); }); $rootscope.$on('$statechangestart', function(event, tostate, toparams){ $log.debug('2 step = use user loaded!'); var _requireauth = tostate.data.requireauth; if(_requireauth && !$rootscope.user){ event.preventdefault(); $state.go('login'); } }); }); ...
db.js file
app.factory('$db', function($q, $cordovasqlite, params...){ ... _self.init = function(){ var deferred = $q.defer(); $cordovasqlite.execute($nddb.open(), "select * user limit 1", []) .then(function(res){ if(res.rows.length > 0){ var _user = { id: res.rows.item(0).id, name: res.rows.item(0).name, ... }; deferred.resolve(_user); } }, function(err){ deferred.reject(err); }); return deferred.promise; ... }; ... });
config.js file
app.config(function($urlrouterprovider, params...){ ... $urlrouterprovider.otherwise('/home'); ... });
the problem is, need $rootscope.user before all, because need authenticate , redirect, go direct home, because $rootscope.user late , $statechangestart no apply redir. if put in $db.init() callback, behaviour same because default route /home , statechangestart load after db.init().
now i'm using bad method, in home check $rootscope.user , redirect /login, it's bad because first see transition between home , login.
if use localstorage, works fine, need use sqlite.
what best approach this?
Comments
Post a Comment