Function

getOwner (object) Object public

Module: @ember/application

Available since v2.3.0

import { getOwner } from '@ember/application';
object
Object
An object with an owner.
returns
Object
An owner object.

Framework objects in an Ember application (components, services, routes, etc.) are created via a factory and dependency injection system. Each of these objects is the responsibility of an "owner", which handled its instantiation and manages its lifetime.

getOwner fetches the owner object responsible for an instance. This can be used to lookup or resolve other class instances, or register new factories into the owner.

For example, this component dynamically looks up a service based on the audioType passed as an argument:

audio.js
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { getOwner } from '@ember/application';

// Usage:
//
//   <PlayAudio @audioType={{@model.audioType}} @audioFile={{@model.file}}/>
//
export default class extends Component {
  get audioService() {
    let owner = getOwner(this);
    return owner.lookup(`service:${this.args.audioType}`);
  }

  @action
  onPlay() {
    let player = this.audioService;
    player.play(this.args.audioFile);
  }
}

© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/3.25/functions/@ember%2Fapplication/getOwner