randombits
randombits

Reputation: 48450

Ember.js: dependencies between two controllers failing

I am trying to access one of two models in a controller that uses needs on a sibling controller. My router looks like the following:

App.Router.map(function() {
    this.route('login');
    this.route('mlb.lineups', {path: 'tools/mlb/lineups'})
    this.resource('mlb.lineups.site', { path: 'tools/mlb/lineups/site/:site_id' });
});

The mlb.lineups route definition looks like the following:

App.MlbLineupsRoute = Ember.Route.extend({
    model: function() {
      var self = this;
      return Ember.RSVP.hash({
        sites: self.store.find('site')
      })
  },

  setupController: function(controller, models) {
    controller.set('model', models.get('sites'));
  },

  afterModel: function(models) {
    var site = models.sites.get('firstObject');
    this.transitionTo('mlb.lineups.site', site);
  }
});

The reason I am using Ember.RSVP.hash({}) here is I plan on adding another model to be retrieved after I retrieve the site model.

Now in my MlbLineupsSiteController I am trying to access the sites model with the following:

App.MlbLineupsSiteController = Ember.ArrayController.extend({
    needs: "mlb.lineups",
    sites: Ember.computed.alias("controllers.models.sites")
});

This is the error I'm getting in my Ember console: needs must not specify dependencies with periods in their names (mlb.lineups)

What's the best way to make the sites model from the MlbLineups controller available in my MlbLineupsSiteController?

Upvotes: 5

Views: 620

Answers (2)

Daniel
Daniel

Reputation: 18672

Note:


@NicholasJohn16's answer isn't valid anymore. It always gives an error that controller couldn't be found. Generally you should also never use needs property and always use Ember.inject.controller if you have to make your controllers dependent on each other. I'd also recommend using services instead of dependencies between controllers. It's easier to maintain code which contains communication between controllers through services, than controller directly accessing other controller's properties. You might not always be aware of such access, and using services gives you another layer of security.

Solution:


Tested in Ember.js 1.10.0-beta.4. Use following code in Controller to reference nested controller in needs:

needs: ['classic/about']

Then you can access it later using:

const aboutController = this.get('controllers.classic/about');
const aboutProperty   = aboutController.get('customProperty');

Works as expected. Basically you need to replace dots with slashes.

Upvotes: 10

NicholasJohn16
NicholasJohn16

Reputation: 2409

It should be:

needs:" MlbLineupsSite "

Basically, the name of the controller you want to include, minus the word controller.

Everything else you posted should work.

Upvotes: 3

Related Questions