Browse files

Make it easy to inject controllers

Add some sugar for injecting controllers into a
state manager.
  • Loading branch information...
1 parent d18768c commit f9681830b7385d2c85722c21409ae7c33da7dbee @wycats wycats committed Apr 30, 2012
View
33 packages/ember-views/lib/system/application.js
@@ -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();
@jbrown
jbrown added a note May 3, 2012

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

@wycats
Ember.js member
wycats added a note May 7, 2012

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

@jbrown
jbrown added a note May 7, 2012

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

@krisselden
Ember.js 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ 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'),
View
30 packages/ember-views/tests/system/application_test.js
@@ -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.