Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you inject a factory function with Aurelia's DI container?

I'd like to make a third-party object on the window injectable so that I can test functionality independent of the third-party service. The component I'm trying to build is a simple wrapper around Disqus embedded comments. The factory I would like to use is as simple as

() => window.DISQUS

Has anyone been able to do this or know if it is possible?

like image 955
lukewestby Avatar asked Nov 19 '25 22:11

lukewestby


1 Answers

Use registerInstance with a string key like this:

container.registerInstance('disqus', window.DISQUS);

Then you can inject the disqus instance into your view models like this:

import {inject} from 'aurelia-framework';

@inject('disqus')
export class Foo {
  constructor(disqus) {
    this.disqus = disqus;
  }
  ...
}
like image 66
Jeremy Danyow Avatar answered Nov 22 '25 11:11

Jeremy Danyow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!