Skip to content

Instantly share code, notes, and snippets.

@neborn
Last active August 8, 2020 00:06
Show Gist options
  • Save neborn/5e03de9784a2da54d9e400cc6c35d1b9 to your computer and use it in GitHub Desktop.
Save neborn/5e03de9784a2da54d9e400cc6c35d1b9 to your computer and use it in GitHub Desktop.
Configurations in the Container
import Component from '@glimmer/component';
import { inject as configuration } from '../utils/configuration';
export default class extends Component {
@configuration
myConfiguration;
get sameConfiguration() {
return this.args.configuration == this.myConfiguration;
}
}
import Configuration from '../utils/configuration';
import { inject as service } from '@ember/service';
export default class MyConfiguration extends Configuration {
@service
myService;
get options() {
return [
this.myService.getOption(0),
this.myService.getOption(1)
];
}
}
import Controller from '@ember/controller';
import { inject as configuration } from '../utils/configuration';
export default class ApplicationController extends Controller {
@configuration
myConfiguration;
}
import Service from '@ember/service';
export default class MyService extends Service {
getOption(idx) {
return {
name: `Option ${idx}`
};
}
}
{{#each this.myConfiguration.options as |option|}}
<p>{{option.name}}</p>
{{/each}}
<MyComponent @configuration={{this.myConfiguration}} />
<p>
The configuration is really a singleton: {{sameConfiguration}}
</p>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
import { getOwner, setOwner } from '@ember/application';
/**
* Base class that matches the containers expectations of a static create method that it will use to instantiate an instance of the class when a lookup is performed
* For an example of an actual configuration class see configurations/my-configuration.js
*/
export default class Configuration {
static create(something) {
const configuration = new this();
setOwner(configuration, getOwner(something));
return configuration;
}
}
/**
* inject property decorator that can be used just like the service inject decorator, but for looking up classes in the components directory instead of the services directory
*/
export function inject(clazz, propertyName, config) {
config.initializer = function() {
return getOwner(this).lookup(`configuration:${propertyName}`);
}
return config;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment