Aureliajs在App构造函数上等待数据 [英] Aureliajs Waiting For Data on App Constructor

查看:52
本文介绍了Aureliajs在App构造函数上等待数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用aureliajs开发一个应用程序.开发过程开始了许多个月,现在,后端开发人员希望对他们的服务进行版本控制.因此,我有一个Web服务可调用以获取每个服务器端(Web api)应用程序的版本,然后针对进一步的请求,调用正确的api地址(包括其版本).

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.

因此,在app.js中,我请求系统元并将其存储在某处.但是某些组件在完成此请求之前已初始化.因此,他们将找不到初始化的版本并请求错误的服务器数据.

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.

我想让app.js构造函数等待,直到检索到该数据.例如这样的东西:

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?

该问题不是此问题的重复项.在这个问题中,外部类中有一个地方等待初始化工作;尽管在我的问题中,主要的问题是等待的位置.因此,问题不仅仅在于构造函数中的异步函数,还在于在异步作业解决之前,阻止所有aurelia作业.

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.

推荐答案

Aurelia提供了许多处理异步流的方法.如果您的自定义元素是路由组件,则可以利用activate生命周期来返回诺言并异步初始化http服务.

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.

否则,在完成初始化之前,可以使用CompositionTransaction进一步停止该过程.您可以在 https://tungphamblog.wordpress上看到一个初步示例. com/2016/08/15/aurelia-customelement-async/

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/

您还可以在启动Aurelia应用程序时利用configure函数的异步特性在此处进行初始化:

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();
}

这篇关于Aureliajs在App构造函数上等待数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆