Ember.getOwner()

getOwner (object) Objectpublic

Defined in packages/container/lib/owner.js:10
Available since 2.3.0

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:

// app/components/play-audio.js
import Ember from 'ember';

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

Parameters:

object Object
An object with an owner.

Returns:

Object
An owner object.
doc_EmberJs
2016-11-30 16:51:28
Comments
Leave a Comment

Please login to continue.