RegistryProxyMixin

RegistryProxyMixin Class

PRIVATE

Defined in: packages/ember-runtime/lib/mixins/registry_proxy.js:11

Module: ember-runtime

RegistryProxyMixin is used to provide public access to specific registry functionality.

hasRegistration (fullName) Booleanpublic

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:112

Check if a factory is registered.

Parameters:

fullName String

Returns:

Boolean

inject (factoryNameOrType, property, injectionName) public

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:205

Define a dependency injection onto a specific factory or all factories of a type.

When Ember instantiates a controller, view, or other framework component it can attach a dependency to that component. This is often used to provide services to a set of framework components.

An example of providing a session object to all controllers:

let App = Ember.Application.create();
let Session = Ember.Object.extend({ isAuthenticated: false });

// A factory must be registered before it can be injected
App.register('session:main', Session);

// Inject 'session:main' onto all factories of the type 'controller'
// with the name 'session'
App.inject('controller', 'session', 'session:main');

App.IndexController = Ember.Controller.extend({
  isLoggedIn: Ember.computed.alias('session.isAuthenticated')
});

Injections can also be performed on specific factories.

App.inject(<full_name or type>, <property name>, <full_name>)
App.inject('route', 'source', 'source:main')
App.inject('route:application', 'email', 'model:email')

It is important to note that injections can only be performed on classes that are instantiated by Ember itself. Instantiating a class directly (via create or new) bypasses the dependency injection system.

Note: Ember-Data instantiates its models in a unique manner, and consequently injections onto models (or all models) will not work as expected. Injections on models can be enabled by setting EmberENV.MODEL_FACTORY_INJECTIONS to true.

Parameters:

factoryNameOrType String
property String
injectionName String

register (fullName, factory, options) public

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:31

Registers a factory that can be used for dependency injection (with inject) or for service lookup. Each factory is registered with a full name including two parts: type:name.

A simple example:

let App = Ember.Application.create();

App.Orange = Ember.Object.extend();
App.register('fruit:favorite', App.Orange);

Ember will resolve factories from the App namespace automatically. For example App.CarsController will be discovered and returned if an application requests controller:cars.

An example of registering a controller with a non-standard name:

let App = Ember.Application.create();
let Session = Ember.Controller.extend();

App.register('controller:session', Session);

// The Session controller can now be treated like a normal controller,
// despite its non-standard name.
App.ApplicationController = Ember.Controller.extend({
  needs: ['session']
});

Registered factories are instantiated by having create called on them. Additionally they are singletons, each time they are looked up they return the same instance.

Some examples modifying that default behavior:

let App = Ember.Application.create();

App.Person = Ember.Object.extend();
App.Orange = Ember.Object.extend();
App.Email = Ember.Object.extend();
App.session = Ember.Object.create();

App.register('model:user', App.Person, { singleton: false });
App.register('fruit:favorite', App.Orange);
App.register('communication:main', App.Email, { singleton: false });
App.register('session', App.session, { instantiate: false });

Parameters:

fullName String
type:name (e.g., 'model:user')
factory Function
(e.g., App.Person)
options Object
(optional) disable instantiation or singleton usage

registerOption (fullName, optionName, options) public

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:122

Register an option for a particular factory.

Parameters:

fullName String
optionName String
options Object

registerOptions (fullName, options) public

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:144

Register options for a particular factory.

Parameters:

fullName String
options Object

registerOptionsForType (type, options) public

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:164

Allow registering options for all factories of a type.

let App = Ember.Application.create();
let appInstance = App.buildInstance();

// if all of type `connection` must not be singletons
appInstance.registerOptionsForType('connection', { singleton: false });

appInstance.register('connection:twitter', TwitterConnection);
appInstance.register('connection:facebook', FacebookConnection);

let twitter = appInstance.lookup('connection:twitter');
let twitter2 = appInstance.lookup('connection:twitter');

twitter === twitter2; // => false

let facebook = appInstance.lookup('connection:facebook');
let facebook2 = appInstance.lookup('connection:facebook');

facebook === facebook2; // => false

Parameters:

type String
options Object

registeredOption (fullName, optionName) Objectpublic

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:133

Return a specific registered option for a particular factory.

Parameters:

fullName String
optionName String

Returns:

Object
options

registeredOptions (fullName) Objectpublic

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:154

Return registered options for a particular factory.

Parameters:

fullName String

Returns:

Object
options

registeredOptionsForType (type) Objectpublic

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:195

Return the registered options for all factories of a type.

Parameters:

type String

Returns:

Object
options

resolveRegistration (fullName) Functionpublic

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:21

Given a fullName return the corresponding factory.

Parameters:

fullName String

Returns:

Function
fullName's factory

unregister (fullName) public

Defined in packages/ember-runtime/lib/mixins/registry_proxy.js:92

Unregister a factory.

let App = Ember.Application.create();
let User = Ember.Object.extend();
App.register('model:user', User);

App.resolveRegistration('model:user').create() instanceof User //=> true

App.unregister('model:user')
App.resolveRegistration('model:user') === undefined //=> true

Parameters:

fullName String

© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/classes/RegistryProxyMixin.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部