Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup bindings between controllers with the new Ember.js router

Tags:

ember.js

Explanation:

Many things have changed with the new Ember.js router. I've migrated most things, but there's one use case that I can't seem to fit into the Ember.js way of doing things.

Say I have an app with some controllers. Some of these controllers belong to the routes in my App.Router and some of them don't, namely my I18nController. This is a non-router controller that makes sure to translate stuff all over the app via a Handlebars helper.

I also have an App.UserController that has an App.User record as its content.

Problem:

Previously, I was able to setup a binding between App.I18nController and App.UserController via the rather ugly localeBinding: 'App.router.userController.content.locale', allowing me to read the user's locale at any time when localizing.

However, with the new router, this is no longer possible.

Question:

My question is: how should I setup this binding using the new router?

like image 253
Kasper Tidemann Avatar asked Oct 21 '22 20:10

Kasper Tidemann


1 Answers

With the new router there are a couple of different strategies for setting up bindings (dependencies) between controllers. One option is to set this.controllerFor in your setupControllers callback in your route defition. Another is to register dependencies for inject prior to app creation. These and a couple of other options are discussed in this github issue. If you follow that thread you'll find code examples for these various strategies. It doesn't seem like a real convention has been established yet but I like the calls like App.container.injection("controller", "settings", "controller:settingsController") approach because it is very declarative and clear.

like image 55
Sean O'Hara Avatar answered Nov 01 '22 13:11

Sean O'Hara