matt
matt

Reputation: 177

How do I create an Aurelia Plugin that's configurable?

I'm creating a new plugin based off the Aurelia Skeleton Plugin. But I don't know how to make it configurable. I want users to set a property (an API path specifically) when they add my plugin. So that whenever they use the plugin it's configured how they need.

Upvotes: 2

Views: 663

Answers (1)

Jeremy Danyow
Jeremy Danyow

Reputation: 26406

  1. Add a second "callback" argument to your plugins configure method:
configure(frameworkConfig, callback) {
  1. Check whether this argument was passed in. If it was, invoke it with your "Configurer" class.
const myPluginConfiguration = new MyPluginConfiguration();
if (callback instanceof Function) {
    callback(myPluginConfiguration);
}
  1. Finally, apply the configuration:
myPluginConfiguration.apply();

The application consuming your plugin would do something like this in their main.js

...
.plugin('my-plugin', config => config.apiPath('https://api.foo.com/'))
...

And your plugin's configuration class might look something like this:

export class MyPluginConfiguration {
  apiPath(path) {
    // do something with path
  }

  apply() {
    // any final configuration...
  }
}

Here's an official example: https://github.com/aurelia/validation/blob/master/src/aurelia-validation.ts#L53

Upvotes: 2

Related Questions