0

I have a Message model which is set up as follows:

App.Message = DS.Model.extend({
    subject   : DS.attr( 'string' ),
    message   : DS.attr( 'string' ),
    deletedBy : DS.attr(),
    createdAt : DS.attr( 'string' ),

    sender     : DS.belongsTo( 'user', {embedded : false, key : 'sender'} ),
    recipient  : DS.belongsTo( 'user', {embedded : false, key : 'recipient'} )
});

In my InboxController, I have set up a dependency with the UsersController. However, when I load the inbox route directly, it is not loading the UsersController model (it's not nested). What is the most appropriate way to get this to work?

InboxController:

App.InboxController = Ember.ArrayController.extend({
    needs: ['users'],
    itemController: 'inboxItem',
    ...
});

Update

Example Fiddle: http://jsfiddle.net/9asHJ/2/

1 Answer 1

1

When you need one controller into another, you can use the needs and get access to the UsersController via this.get('controllers.users.something')

Docs

Sample Fiddle

Incase, you need some controller or model to be injected to all controllers or routes or something, you can perform dependency injection during app initialization.

Refer docs

Sign up to request clarification or add additional context in comments.

3 Comments

In your example, the isAdmin attribute is being set by the controller. However, in my setup the model is set up by the router. I created a fiddle and added it to the question. Is injecting the model into all controllers the only solution for this problem? Thanks!
A route#model hook will get called only when you visit that route.
One thing i can suggest you is that you fetch that model from the applicationRoute. It's the only route that gets invoked whereever your app boots. jsfiddle.net/selvaG/9asHJ/4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.