Permalink
Browse files

Make it easy to inject controllers

Add some sugar for injecting controllers into a
state manager.
  • Loading branch information...
wycats committed May 1, 2012
1 parent d18768c commit f9681830b7385d2c85722c21409ae7c33da7dbee
@@ -83,6 +83,39 @@ Ember.Application = Ember.Namespace.extend(
}
},
/**
Instantiate all controllers currently available on the namespace
and inject them onto a state manager.
Example:
App.PostsController = Ember.ArrayController.create();
App.CommentsController = Ember.ArrayController.create();

This comment has been minimized.

Show comment
Hide comment
@jbrown

jbrown May 3, 2012

Contributor

@wycats Shouldn't these be extend instead of create?

@jbrown

jbrown May 3, 2012

Contributor

@wycats Shouldn't these be extend instead of create?

This comment has been minimized.

Show comment
Hide comment
@wycats

wycats May 7, 2012

Member

Nope. The purpose of this method is to instantiate and inject controller instances.

@wycats

wycats May 7, 2012

Member

Nope. The purpose of this method is to instantiate and inject controller instances.

This comment has been minimized.

Show comment
Hide comment
@jbrown

jbrown May 7, 2012

Contributor

Check out #796. The jsFiddle will show you what I'm talking about.

@jbrown

jbrown May 7, 2012

Contributor

Check out #796. The jsFiddle will show you what I'm talking about.

This comment has been minimized.

Show comment
Hide comment
@krisselden

krisselden May 7, 2012

Member

If the purpose is to instantiate then you do mean extend not create which matches the test.

@jbrown is referring to the documented example which uses create not extend unlike the test f968183#L1R76

If you follow the example, you'd get an error when injectControllers tries to call create. The documented example needs to be updated.

@krisselden

krisselden May 7, 2012

Member

If the purpose is to instantiate then you do mean extend not create which matches the test.

@jbrown is referring to the documented example which uses create not extend unlike the test f968183#L1R76

If you follow the example, you'd get an error when injectControllers tries to call create. The documented example needs to be updated.

var stateManager = Ember.StateManager.create({
...
});
App.injectControllers(stateManager);
stateManager.get('postsController') // <App.PostsController:ember1234>
stateManager.get('commentsController') // <App.CommentsController:ember1235>
stateManager.getPath('postsController.stateManager') // stateManager
*/
injectControllers: function(stateManager) {
var properties = Ember.A(Ember.keys(this)),
namespace = this, controller, name;
properties.forEach(function(property) {
if (!/^[A-Z].*Controller$/.test(property)) { return; }
name = property[0].toLowerCase() + property.substr(1);
controller = namespace[property].create();
stateManager.set(name, controller);
controller.set('stateManager', stateManager);
});
},
/** @private */
didBecomeReady: function() {
var eventDispatcher = get(this, 'eventDispatcher'),
@@ -7,7 +7,7 @@
var view;
var application;
var set = Ember.set, get = Ember.get;
var set = Ember.set, get = Ember.get, getPath = Ember.getPath;
module("Ember.Application", {
setup: function() {
@@ -61,3 +61,31 @@ test("acts like a namespace", function() {
app.destroy();
window.TestApp = undefined;
});
var app;
module("Ember.Application injection", {
teardown: function() {
Ember.run(function(){ app.destroy(); });
}
});
test("inject controllers into a state manager", function() {
app = Ember.Application.create();
app.FooController = Ember.Object.extend();
app.BarController = Ember.ArrayController.extend();
app.Foo = Ember.Object.create();
app.fooController = Ember.Object.create();
var stateManager = Ember.Object.create();
app.injectControllers(stateManager);
ok(get(stateManager, 'fooController') instanceof app.FooController, "fooController was assigned");
ok(get(stateManager, 'barController') instanceof app.BarController, "barController was assigned");
ok(get(stateManager, 'foo') === undefined, "foo was not assigned");
equal(getPath(stateManager, 'fooController.stateManager'), stateManager, "the state manager is assigned");
equal(getPath(stateManager, 'barController.stateManager'), stateManager, "the state manager is assigned");
});

0 comments on commit f968183

Please sign in to comment.