App.js and routes.js AFTER localforage.getItem

Hi there!

I’m loading the user id with following code into a js variable:

localforage.getItem('userid').then(function (value) {
    userid = value;
}).catch(function (err) {
    console.log(err);
});

Inside app.js and especially inside routes.js I’m using the userid to get the correct user-data. Depending on this data I’m routing the user to ether page A or page B.

How can I load routes.js and app.js after the userid is retrieved from localforage?

Thank you very much!

Read the doc on routes redirect.

https://framework7.io/docs/routes.html#redirect-alias

I’m not on my PC. If you need more help I can write an example tomorrow

One way is to put that User ID code in the App.init() function in app.js. This is where your app is initialized and this function will have to run before anything else, including routing.

So, if you are able to retrieve the User ID when the app is being initialized, you will then be able to use the same User ID later in the app.

That would be awesome! Thank you in advance @pvtallulah !

Thank you for your answer! How do I use the app.init() function in app.js? @tsalira

I think this should be able to give you what you want:

var app = new Framework7({
root: '#app',
id: 'App bundle ID',
name: 'App name', 
theme: 'auto',
version: "1.0.0",
// App root data
data: function () {
return {
  // app data
  };
},

on: {
init: function () {
  var f7 = this;
  if (f7.device.cordova) {
    // Init cordova APIs (see cordova-app.js)
    cordovaApp.init(f7);
  }

  // Your user id code here...
  localforage.getItem('userid').then(function (value) {
      userid = value;
  }).catch(function (err) {
      console.log(err);
  });

  // More init code...
  // ...
  // ...

  // You can use your user id data here
    }
  }
});

Thank you for your answer. The problem is still, that the localforage API is async. So the code is still executed AFTER routes.js is loaded…
Do you have another idea? Or is there a function I can call within localforage to init the app? @tsalira

localStorage.getItem() not async

In my app it is: https://github.com/localForage/localForage#callbacks-vs-promises
How can I make it sync (not async)?

var app;
localforage.getItem('userid').then(function (value) {
    userid = value;
    app = new Framework7({
      // ...
    });
}).catch(function (err) {
    console.log(err);
});