# Loading a xml file once

**URL:** <https://discourse.aurelia.io/t/loading-a-xml-file-once/1456>\
**Category:** Help Requests\
**Created:** [July 20, 2018, 10:01am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456 "2018-07-20T10:01:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)\
**Post date:** [July 20, 2018, 10:01am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/1 "2018-07-20T10:01:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)\
**Post date:** [July 23, 2018, 8:29am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/2 "2018-07-23T08:29:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [July 23, 2018, 9:39am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/3 "2018-07-23T09:39:00Z")

</div>

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

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

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

```

Additionally there are [two](https://github.com/Vheissu/aurelia-configuration) already existing [plugins](https://github.com/SpoonX/aurelia-config) which might do exactly what you want.

---

<div class="post-metadata">

**Author:** ![dnkm](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/dnkm/32/594_2.png) [@dnkm](https://discourse.aurelia.io/u/dnkm)\
**Post date:** [July 23, 2018, 9:40am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/4 "2018-07-23T09:40:05Z")

</div>

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?

```javascript
$.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);
    });
});

```

---

<div class="post-metadata">

**Author:** ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)\
**Post date:** [July 23, 2018, 9:42am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/5 "2018-07-23T09:42:26Z")

</div>

> [@zewa666](#):
>
> Additionally there are two already existing plugins which might do exactly what you want.

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.

---

<div class="post-metadata">

**Author:** ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)\
**Post date:** [July 23, 2018, 9:44am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/6 "2018-07-23T09:44:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [July 23, 2018, 9:44am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/7 "2018-07-23T09:44:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [July 23, 2018, 9:46am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/8 "2018-07-23T09:46:58Z")

</div>

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

```javascript
export class MyGlobalContainer {
  ...
}

```

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

```

---

<div class="post-metadata">

**Author:** ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)\
**Post date:** [July 23, 2018, 9:46am UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/9 "2018-07-23T09:46:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dnkm](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/dnkm/32/594_2.png) [@dnkm](https://discourse.aurelia.io/u/dnkm)\
**Post date:** [July 23, 2018, 5:58pm UTC](https://discourse.aurelia.io/t/loading-a-xml-file-once/1456/10 "2018-07-23T17:58:31Z")

</div>

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**

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

```

and then this in your view model

**management.js**

```javascript
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…
