dimanche 29 novembre 2015

Ionic/Angular/SQLite - How to load a specific view based on the table data of a pre-filled database?

When my app loads, I like to load a specific view based on the data stored on a SQLite database table. My database structure and my scenario is as below:

SQLite Database: myapp.db
Table: settings
Table fields: api_key, active_user_id

When the app loads, I would like to fetch the data of 'api_key' and 'active_user_id' from the SQLite table.

Scenario 1: if the 'api_key' is null or '', I need to load the 'registration' view.

Scenario 2: if the 'api_key' is 'not empty' and the 'active_user_id' is 'empty' or '0', then I need to load the 'login' view

Scenario 3: if the 'api_key' is 'not empty' and the 'active_user_id' is > 0, then I need to load the 'dashboard' view.

Here is my app.js

(function() {
  
  var app = angular.module('tracking', ['ionic', 'ngCordova']);
  var db = null;

  app.controller('RegistrationCtrl', function($scope, $cordovaSQLite) {
    //properties and functions goes in here
  });
  
  app.controller('LoginCtrl', function($scope, $cordovaSQLite) {
    //properties and functions goes in here
  });
  
  app.controller('DashboardCtrl', function($scope, $cordovaSQLite) {
    //properties and functions goes in here
  });

  app.run(function($ionicPlatform, $cordovaSQLite) {
    $ionicPlatform.ready(function() {
      if(window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      }
      if(window.StatusBar) {
        StatusBar.styleDefault();
      }

      window.plugins.sqlDB.copy("tracking.db", 0, function() {
        db = $cordovaSQLite.openDB("tracking.db");
      }, function(err) {
        db = $cordovaSQLite.openDB("tracking.db");
      });

    });
  });

  app.config(function($stateProvider, $urlRouterProvider) {

    $stateProvider
        .state('registration', {
          url: '/registration',
          templateUrl: 'templates/registration.html',
          controller: 'RegistrationCtrl'
        })
        .state('login', {
          url: '/login',
          templateUrl: 'templates/login.html',
          controller: 'LoginCtrl'
        })
        .state('dashboard', {
          url: '/dashboard',
          templateUrl: 'templates/dashboard.html',
          controller: 'DashboardCtrl'
        });

    $urlRouterProvider.otherwise('/registration');
  });

}());

Where do I write the code to fetch the value of the api_key and active_user_id when the app loads?

How do I load a specific view based on the value of api_key and active_user_id, as per the scenarios mentioned above?

Any help would be really appreciated.

Aucun commentaire:

Enregistrer un commentaire