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