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 attribute:

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

// Usage:
//
//   {{play-audio audioType=model.audioType audioFile=model.file}}
//
export default Component.extend({
  audioService: computed('audioType', function() {
    let owner = getOwner(this);
    return owner.lookup(`service:${this.get('audioType')}`);
  }),

  click() {
    let player = this.get('audioService');
    player.play(this.get('audioFile'));
  }
});

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