Thanks for the answer !
If I declare the database connexion in the main run() function, how can I access it globally ?
For example in my service, Iâll have to do something like :
Inside an init method of your service. The main run function calls the init method on the service once the platform is ready. You wonât access the database directly from anywhere outside the service itself. The service is a wrapper for the sqlitePlugin methods. For instance, your service would expose a query method that runs the transaction on the database. Everywhere else in the app, youâll inject the service and run the query method. That way if you changed plugins or database back-ends, you could just change the service itself, and the rest of your app will work with it.
So basically I want to use promise in my DB.query function, and then use this function in my Resources services (User as an example here).
And I want my Resources services to return promises too to map my $scope variables with the result of the query function.
Any idea ?
At first glance, I see that your query function is indeed returning a promise, but that promise is never resolved. You also are not actually processing the data response from the executeSql call. You might want that block to look something like this:
You should also be prepared to reject the promise if your query goes awry, and handle that rejection further upstream. Read up on angular promises for more info: http://docs.angularjs.org/api/ng/service/$q
Haha I forgot to resolve it, indeed !
Thank you for all the help provided, here is my working service if anyone want to use it https://gist.github.com/jgoux/10738978
Thanks, it works perfectly.
Just to add for anyone new to this topic: for iOS the DB.init() must be called after the platformReady. Something in the line:
.controller(âMyControllerâ, function($scope, $ionicPlatform, $state,DBFactoryObj) {
$ionicPlatform.ready(function() {
// Platform stuff here.
DBFactoryObj.initDB();// the factory method will call the database initialization code, DB.init()
});
}