Ember.ControllerMixin Class packages/ember-views/lib/system/controller.js:11
Defined in: packages/ember-views/lib/system/controller.js:11
Module: ember-views
Additional methods for the ControllerMixin
Methods
- child
- controllerFor
- describe
- destroy
- eachLocal
- factoryInjection
- factoryTypeInjection
- get
- has
- injection
- lookup
- lookupFactory
- makeToString
- normalize
- options
- optionsForType
- register
- remove
- replaceRoute
- replaceWith
- reset
- resolve
- set
- transitionTo
- transitionToRoute
- typeInjection
- unregister
Properties
child
Container
Returns a new child of the current container. These children are configured to correctly inherit from the current container.
Returns:
- Container
controllerFor
deprecated
describe
A hook that can be used to describe how the resolver will attempt to find the factory.
For example, the default Ember .describe
returns the full
class name (including namespace) where Ember's resolver expects
to find the fullName
.
destroy
A depth first traversal, destroying the container, its descendant containers and all their managed objects.
eachLocal
(callback, binding)
Iterate and invoke a callback for each local key-value pair.
Parameters:
- callback Function
- binding Object
factoryInjection
(factoryName, property, injectionName)
Defines factory injection rules.
Similar to regular injection rules, but are run against factories, via
Container#lookupFactory
.
These rules are used to inject objects onto factories when they are looked up.
Two forms of injections are possible:
1 2 |
* Injecting one fullName on another fullName * Injecting one fullName on a type |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
var container = new Container(); container.register('store:main', Store); container.register('store:secondary', OtherStore); container.register('model:user', User); container.register('model:post', Post); // injecting one fullName on another type container.factoryInjection('model', 'store', 'store:main'); // injecting one fullName on another fullName container.factoryInjection('model:post', 'secondaryStore', 'store:secondary'); var UserFactory = container.lookupFactory('model:user'); var PostFactory = container.lookupFactory('model:post'); var store = container.lookup('store:main'); UserFactory.store instanceof Store; //=> true UserFactory.secondaryStore instanceof OtherStore; //=> false PostFactory.store instanceof Store; //=> true PostFactory.secondaryStore instanceof OtherStore; //=> true // and both models share the same source instance UserFactory.store === PostFactory.store; //=> true |
factoryTypeInjection
(type, property, fullName)
private
get
Any
Retrieve the value given a key, if the value is present at the current level use it, otherwise walk up the parent hierarchy and try again. If no matching key is found, return undefined.
Returns:
- Any
has
(fullName)
Boolean
Given a fullName check if the container is aware of its factory or singleton instance.
Parameters:
- fullName String
Returns:
- Boolean
injection
(factoryName, property, injectionName)
Defines injection rules.
These rules are used to inject dependencies onto objects when they are instantiated.
Two forms of injections are possible:
1 2 |
* Injecting one fullName on another fullName * Injecting one fullName on a type |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
var container = new Container(); container.register('source:main', Source); container.register('model:user', User); container.register('model:post', Post); // injecting one fullName on another fullName // eg. each user model gets a post model container.injection('model:user', 'post', 'model:post'); // injecting one fullName on another type container.injection('model', 'source', 'source:main'); var user = container.lookup('model:user'); var post = container.lookup('model:post'); user.source instanceof Source; //=> true post.source instanceof Source; //=> true user.post instanceof Post; //=> true // and both models share the same source user.source === post.source; //=> true |
lookup
(fullName, options)
Any
Given a fullName return a corresponding instance.
The default behaviour is for lookup to return a singleton instance. The singleton is scoped to the container, allowing multiple containers to all have their own locally scoped singletons.
1 2 3 4 5 6 7 8 9 10 11 12 |
var container = new Container(); container.register('api:twitter', Twitter); var twitter = container.lookup('api:twitter'); twitter instanceof Twitter; // => true // by default the container will return singletons twitter2 = container.lookup('api:twitter'); twitter instanceof Twitter; // => true twitter === twitter2; //=> true |
If singletons are not wanted an optional flag can be provided at lookup.
1 2 3 4 5 6 7 |
var container = new Container(); container.register('api:twitter', Twitter); var twitter = container.lookup('api:twitter', { singleton: false }); var twitter2 = container.lookup('api:twitter', { singleton: false }); twitter === twitter2; //=> false |
Parameters:
- fullName String
- options Object
Returns:
- Any
lookupFactory
(fullName)
Any
Given a fullName return the corresponding factory.
Parameters:
- fullName String
Returns:
- Any
makeToString
(factory, fullName)
Function
Parameters:
- factory Any
- fullName String
Returns:
- Function
- toString function
normalize
(fullName)
String
A hook to enable custom fullName normalization behaviour
Parameters:
- fullName String
Returns:
- String
- normalized fullName
optionsForType
(type, options)
Allow registering options for all factories of a type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var container = new Container(); // if all of type `connection` must not be singletons container.optionsForType('connection', { singleton: false }); container.register('connection:twitter', TwitterConnection); container.register('connection:facebook', FacebookConnection); var twitter = container.lookup('connection:twitter'); var twitter2 = container.lookup('connection:twitter'); twitter === twitter2; // => false var facebook = container.lookup('connection:facebook'); var facebook2 = container.lookup('connection:facebook'); facebook === facebook2; // => false |
Parameters:
- type String
- options Object
register
(fullName, factory, options)
Registers a factory for later injection.
Example:
1 2 3 4 5 |
var container = new Container(); container.register('model:user', Person, {singleton: false }); container.register('fruit:favorite', Orange); container.register('communication:main', Email, {singleton: false}); |
replaceRoute
(name, models)
Transition into another route while replacing the current URL, if possible.
This will replace the current history entry instead of adding a new one.
Beside that, it is identical to transitionToRoute
in all other respects.
1 2 |
aController.replaceRoute('blogPosts'); aController.replaceRoute('blogPosts.recentEntries'); |
Optionally supply a model for the route in question. The model
will be serialized into the URL using the serialize
hook of
the route:
1 |
aController.replaceRoute('blogPost', aPost);
|
Multiple models will be applied last to first recursively up the resource tree.
1 2 3 4 5 6 |
this.resource('blogPost', {path:':blogPostId'}, function(){ this.resource('blogComment', {path: ':blogCommentId'}); }); aController.replaceRoute('blogComment', aPost, aComment); |
Parameters:
- name String
- the name of the route
- models ...Object
- the model(s) to be used while transitioning to the route.
replaceWith
deprecated
reset
resolve
(fullName)
Function
Given a fullName return the corresponding factory.
By default resolve
will retrieve the factory from
its container's registry.
1 2 3 4 |
var container = new Container(); container.register('api:twitter', Twitter); container.resolve('api:twitter') // => Twitter |
Optionally the container can be provided with a custom resolver.
If provided, resolve
will first provide the custom resolver
the oppertunity to resolve the fullName, otherwise it will fallback
to the registry.
1 2 3 4 5 6 7 |
var container = new Container(); container.resolver = function(fullName) { // lookup via the module system of choice }; // the twitter factory is added to the module system container.resolve('api:twitter') // => Twitter |
Parameters:
- fullName String
Returns:
- Function
- fullName's factory
set
(object, key, value)
Sets a key-value pair on the current container. If a parent container, has the same key, once set on a child, the parent and child will diverge as expected.
Parameters:
- object Object
- key String
- value Any
transitionTo
deprecated
transitionToRoute
(name, models)
Transition the application into another route. The route may be either a single route or route path:
1 2 |
aController.transitionToRoute('blogPosts'); aController.transitionToRoute('blogPosts.recentEntries'); |
Optionally supply a model for the route in question. The model
will be serialized into the URL using the serialize
hook of
the route:
1 |
aController.transitionToRoute('blogPost', aPost);
|
Multiple models will be applied last to first recursively up the resource tree.
1 2 3 4 5 6 |
this.resource('blogPost', {path:':blogPostId'}, function(){ this.resource('blogComment', {path: ':blogCommentId'}); }); aController.transitionToRoute('blogComment', aPost, aComment); |
See also 'replaceRoute'.
Parameters:
- name String
- the name of the route
- models ...Object
- the model(s) to be used while transitioning to the route.
typeInjection
(type, property, fullName)
private
unregister
(fullName)
Unregister a fullName
1 2 3 4 5 6 7 |
var container = new Container(); container.register('model:user', User); container.lookup('model:user') instanceof User //=> true container.unregister('model:user') container.lookup('model:user') === undefined //=> true |
Parameters:
- fullName String
_options
InheritingDict
private
Default: null
_typeOptions
InheritingDict
private
cache
InheritingDict
children
Array
Default: []
controllers
Object
Stores the instances of other controllers available from within
this controller. Any controller listed by name in the needs
property will be accessible by name through this property.
1 2 3 4 5 6 7 |
App.CommentsController = Ember.ArrayController.extend({ needs: ['post'], postTitle: function(){ var currentPost = this.get('controllers.post'); // instance of App.PostController return currentPost.get('title'); }.property('controllers.post.title') }); |
Default: null
dict
Object
Object used to store the current nodes data.
Default: Object
injections
Object
Default: {}
needs
Array
An array of other controller objects available inside
instances of this controller via the controllers
property:
For example, when you define a controller:
1 2 3 |
App.CommentsController = Ember.ArrayController.extend({ needs: ['post'] }); |
The application's single instance of these other
controllers are accessible by name through the
controllers
property:
1 |
this.get('controllers.post'); // instance of App.PostController |
This is only available for singleton controllers.
Default: []
parent
Container
Default: null
registry
InheritingDict
resolver
function
target
The object to which actions from the view should be sent.
For example, when a Handlebars template uses the {{action}}
helper,
it will attempt to send the action to the view's controller's target
.
By default, a controller's target
is set to the router after it is
instantiated by Ember.Application#initialize
.
Default: null
typeInjections
InheritingDict