Aureliajs Waiting For Data on App Constructor

三世轮回 提交于 2020-01-03 13:43:48

问题


I am developing an app in aureliajs. The development process is started for many months and now, the back-end developers want to make their services versioned. So I have a web service to call to get the version of each server side (web api) app and then, for the further requests, call the right api address including its version.

So, in the app.js I am requesting the system meta and storing it somewhere. But some components get initialized before this request gets done. So they won't find the version initialized and requesting the wrong server data.

I want to make the app.js constructor wait until this data is retrieved. For example something like this:

export class App {
  async constructor(...) {
    ...

    await this.initializeHttp();

    ...
  }

  initializeHttp(){
    // get the system meta from server
  }
}

but this solution is not applicable. Because the constructor can't be async. So how should I block the job until the system meta is retrieved?

UPDATE

The question is not a duplicate of this question. In that question, there is a place in outer class to await for the initialization job; although in my question, the main problem is, where to put this await-tion. So the question is not just about async function in constructor, but is about blocking all aurelia jobs until async job resolves.


回答1:


Aurelia provides many ways to handle asynchronous flow. If your custom element is a routed component, then you can leverage activate lifecycle to return a promise and initialize the http service asynchronously.

Otherwise, you can use CompositionTransaction to halt the process further, before you are done with initialization. You can see a preliminary example at https://tungphamblog.wordpress.com/2016/08/15/aurelia-customelement-async/

You can also leverage async nature of configure function in bootstrapping an Aurelia application to do initialization there:

export function configure(aurelia) {
  ...
  await aurelia.container.get(HttpServiceInitializer).initialize();
}


来源:https://stackoverflow.com/questions/55220012/aureliajs-waiting-for-data-on-app-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!