Last active
August 29, 2015 14:22
-
-
Save justinfagnani/6aec137ed97cfa3db002 to your computer and use it in GitHub Desktop.
Dependency Resolution with Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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