Skip to content

Instantly share code, notes, and snippets.

@justinfagnani
Last active August 29, 2015 14:22
Show Gist options
  • Save justinfagnani/6aec137ed97cfa3db002 to your computer and use it in GitHub Desktop.
Save justinfagnani/6aec137ed97cfa3db002 to your computer and use it in GitHub Desktop.
Dependency Resolution with Events
Polymer({
is: 'x-foo',
ready() {
// provides a FooService to descendents
this.provideInstance('foo-service', new FooService());
},
attached() {
// requests a BarService from an ancestor, re-requests if moved
let barService = this.requestInstance('bar-service');
},
requestInstance(key) {
let event = this.fire('request-instance', {
key: key,
});
return event.detail.response;
},
provideInstance(key, instance) {
this.addEventListener('request-instance', function(e) {
if (e.detail.key === key) {
e.detail.response = instance;
}
}.bind(this);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment