Loading a xml file once

I need to load an xml file for my individual configuration-service once.
I have a class configuration-service and in constructur I tried to parse the xml file with httpClient and $.get(…). But due to the async the information are not present when I need them.

Can somebody give me an example how I read files to have the information during the whole runtime?

Does noone has an idea?

The requirement is, that an xml file needs to get loaded on aurelia startup, so that the included configurations are always present.

So essentially what you want is to make sure the xml file is loaded before the app starts right?

export function configure(aurelia) {
      aurelia.use
        .standardConfiguration()
        .developmentLogging();

       // Modification
       yourAsyncJobToReadTheXMLFile().then(() => {
        aurelia.start().then(() => aurelia.setRoot());
      });
    }

Additionally there are two already existing plugins which might do exactly what you want.

2 Likes

Can you just put the Aurelia startup code after the $.get has resolved? I assume because you’re using $.get, you are using jQuery, so can you do something like this?

$.get( <<endpoint>>, function( data ) {

  // Do what you need here with your data

    aurelia.start().then(() => {
        let auth = aurelia.container.get(AuthService);
        let root = auth.isAuthenticated() ? "app" : "login";
        aurelia.setRoot(root);
    });
});
2 Likes

Great. I’ll try it.

I’m already using the aurelia-configuration plugin, but as far as I can see, it doesn’t support xml. Due to we are using an IIS Server I would like to use the basic web.config file instead of any json.

Having a global “Configuration-Class” including the data. How can I create a global container? Do you have an example for me? :slight_smile:

Yeah, so essentially make sure to call aurelia.start only once the loading of your xml file is done

well with aurelia you can access the DI container and pretty much register whatever you want

export class MyGlobalContainer {
  ...
}
import { MyGlobalContainer } from "./my-global-container";

export function configure(aurelia) {
      aurelia.use
        .standardConfiguration()
        .developmentLogging();


       // yourAsyncJobToReadTheXMLFile creates a new MyGlobalContainer containing the parsed info from your xml and resolves the promise with the instance
       yourAsyncJobToReadTheXMLFile().then((containerInstance) => {
          // see docshttps://aurelia.io/docs/fundamentals/dependency-injection#how-aurelia-uses-containers
         aurelia.container.registerInstance(MyGlobalContainer, containerInstance);

        aurelia.start().then(() => aurelia.setRoot());
      });
    }
1 Like

Can you give me an example how I can instantiate a container of my “configuration-class” that I can use for the total application?

Currently I have no Idea how to implement that. I’m quite new in Aurelia.

If you have configuration-class.js and import that at the top of each view model that requires access to it, you should be fine. By default, Aurelia will treat it as a singleton, so you should get the same instance everywhere.

If you don’t want to import it everywhere (although I would recommend you only import it when you need it because do you really need it everywhere?), you could always create some kind of base class that imports your configuration, then just extend that for each view/view model.

Like this maybe?

configuration-base-class.js

export class ConfigurationBaseClass{
     apiEndpoint = "https://whatever.com/v1";
}

and then this in your view model

management.js

import { ConfigurationBaseClass } from "./configuration";

export class Management extends ConfigurationBaseClass {
     constructor(){
          super();
          console.log(this.apiEndPoint); // Here is the property from the base class
     }
}

Not sure if that’s what you’re after, and my syntax may be a bit off, but you get the idea…

1 Like