Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document getOwner and setOwner #12562

Merged
merged 1 commit into from
Nov 6, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/container/lib/owner.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,66 @@
/**
@module ember
@submodule ember-runtime
*/

import { symbol } from 'ember-metal/utils';

export const OWNER = symbol('OWNER');


/**
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'));
}
});
```

@method getOwner
@param {Object} object A object with an owner.
@return {Object} an owner object.
@for Ember
@public
*/
export function getOwner(object) {
return object[OWNER];
}

/**
`setOwner` forces a new owner on a given object instance. This is primarily
useful in some testing cases.

@method setOwner
@param {Object} object A object with an owner.
@return {Object} an owner object.
@for Ember
@public
*/
export function setOwner(object, owner) {
object[OWNER] = owner;
}