最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论