javascript - Ember.js: dependencies between two controllers failing - Stack Overflow

I am trying to access one of two models in a controller that uses needs on a sibling controller. My rou

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: Emberputed.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?

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.puted.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?

Share Improve this question asked Apr 29, 2014 at 2:40 randombitsrandombits 48.6k79 gold badges273 silver badges449 bronze badges 1
  • What issues were you facing when you had 'mlb.lineups' as resource? it doesn't make since for one to depend on the other, yet not be guaranteed to be initiated (aka if a user navigated straight to tools/mlb/lineups/site/1, 'mlb.lineups.site' should be a child of 'mlb.lineups'. – Kingpin2k Commented Apr 29, 2014 at 2:52
Add a ment  | 

2 Answers 2

Reset to default 10

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 remend using services instead of dependencies between controllers. It's easier to maintain code which contains munication 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744647172a4585682.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信