#Dependency injection in Octane?

1 messages · Page 1 of 1 (latest)

weary tendon
#

In Ember Classic, container access was like cooties - anything with it could give it to something else in one line.

model() {
  const m = getOwner(this).factoryFor('model:foo').create();
  // m can inject services
}

Is there a similarly concise idiom for Ember Octane? The following works, but it's much more wordy.

model () {
  const owner = getOwner(this);
  const Foo = owner.factoryFor('model:foo').class;
  const m = new Foo();
  setOwner(foo, owner);
}

Is it safer now to use module import syntax in situations like this? I used to avoid it like the plague in Ember Classic because it didn't work with dependency injection. Going that route you get something slightly more palatable, and probably better language server support.

import Foo from 'models/foo'; // 🐉?

model () {
  const m = new Foo();
  setOwner(foo, getOwner(this));
}
lethal canopy
#

using imports will give you intellisense and such in your editor, whereas going through the owner will not

#

your last example is perfect, actually ❤️