I'm looking for solution for load data from sqlite in cordova bootstrap, for more specifically in $ionicPlatform.ready but until here I don't have a solution. See a piece of my code (I'm using ionic with $cordovaSQLite):
app.js file
...
app.run(function($DB, $log, params...){
$ionicPlatform.ready(function() {
...
$DB.init().then(function(res){
// here I need load a 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 * FROM 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, I need $rootScope.user before all, because I need authenticate and redirect, but always I go direct to home, because $rootScope.user is already late and $stateChangeStart no apply redir. If put it in $DB.init() callback, the behaviour is the same because default route is /home and stateChangeStart load after Db.init().
Now I'm using a bad method, in home I check $rootScope.user and redirect to /login, but it's so bad because first we see the transition between home and login.
If I use the localStorage, it works fine, but I really need to use the sqlite.
What the best approach for this? Thanks