Permalink
Browse files

Add connectControllers convenience

  • Loading branch information...
Tom Dale
Tom Dale committed Jul 22, 2012
1 parent d4a9e46 commit 396c08b1322f4b642a65005cc89cdd7bb8acce06
@@ -137,6 +137,28 @@ Ember.ControllerMixin.reopen({
set(this, outletName, view);
return view;
},
/**
Convenience method to connect controllers. This method makes other controllers
available on the controller the method was invoked on.
For example, to make the `personController` and the `postController` available
on the `overviewController`, you would call:
overviewController.connectControllers('person', 'post');
@param {String...} controllerNames the controllers to make available
*/
connectControllers: function() {
var controllers = get(this, 'controllers'),
controllerNames = Array.prototype.slice.apply(arguments),
controllerName;
for (var i=0, l=controllerNames.length; i<l; i++) {
controllerName = controllerNames[i] + 'Controller';
set(this, controllerName, get(controllers, controllerName));
}
}
});
@@ -167,3 +167,19 @@ test("if the controller is not given while connecting an outlet, the instantiate
equal(view.get('controller'), postController, "the controller was inherited from the parent");
});
test("connectControllers injects other controllers", function() {
var postController = {}, commentController = {};
var controller = Ember.Controller.create({
controllers: {
postController: postController,
commentController: commentController
}
});
controller.connectControllers('post', 'comment');
equal(controller.get('postController'), postController, "should connect postController");
equal(controller.get('commentController'), commentController, "should connect commentController");
});

4 comments on commit 396c08b

@trek

This comment has been minimized.

Show comment
Hide comment
@trek

trek Jul 22, 2012

Member

I'm head scratching over the use case on this one...

Member

trek replied Jul 22, 2012

I'm head scratching over the use case on this one...

@tchak

This comment has been minimized.

Show comment
Hide comment
@tchak

tchak Jul 22, 2012

Member

It is very useful, but I am not sure when we supposed to call it. It would not make sens to call it several times (it will not hurt though)

Member

tchak replied Jul 22, 2012

It is very useful, but I am not sure when we supposed to call it. It would not make sens to call it several times (it will not hurt though)

@ghempton

This comment has been minimized.

Show comment
Hide comment
@ghempton

ghempton Jul 23, 2012

Member

Where would you call this? Seems like it would be more straightforward to just have a bound property, e.g. postsControllerBinding: 'controllers.postsController'.

Also, @wycats once mentioned that controllers shouldn't reference each other explicitly, instead they should coordinate through the router. What is a use case for this?

Member

ghempton replied Jul 23, 2012

Where would you call this? Seems like it would be more straightforward to just have a bound property, e.g. postsControllerBinding: 'controllers.postsController'.

Also, @wycats once mentioned that controllers shouldn't reference each other explicitly, instead they should coordinate through the router. What is a use case for this?

@voy

This comment has been minimized.

Show comment
Hide comment
@voy

voy Aug 2, 2012

Can anyone please give some source for how controllers should coordinate through the router? I'd be interested in how that works.

voy replied Aug 2, 2012

Can anyone please give some source for how controllers should coordinate through the router? I'd be interested in how that works.

Please sign in to comment.