0

I'm using ember-cli for an emberjs project. I want a property on my controller to be an alias for a property on my route. I can't find the right way to set it up, and the property is always undefined.

Here's my router:

Router.map(function() {
  this.resource('surveys', function() {
    this.route('editCompletedSurvey', { path: '/:survey_id/:completed_survey_id' });   
  });
});

And routes/surveys/edit-completed-survey.js:

import NewCompletedSurvey from 'myapp/routes/surveys/new-completed-survey';

export default NewCompletedSurvey.extend({
  completedSurveyId: null,

  model: function(params) {
    this.set('completedSurveyId', params.completed_survey_id);
    console.log('Set completedSurveyId to ' + params.completed_survey_id);    // gets set correctly
    return this.store.find('survey', params.survey_id);
  }
});

And finally my controller:

export default Ember.ObjectController.extend({
  completedSurveyId: Ember.computed.alias('SurveysEditCompletedSurveyRoute.completedSurveyId'),    // always 'undefined'. What should this be?
  ...

Can anyone tell me what the value of the string passed to alias should be?

The reason I'm doing this is that in my controller I need to access the value of the :completed_survey_id parameter. I can't use setupController because that is only called once when the route is first accessed, and I need the controller to be able to access this parameter's value as the user selects different surveys to view. If there's a better way to do that, please tell me (note: completed_survey_id isn't the ID of a survey model instance, but is an ID of a different model).

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
jbrown
  • 7,518
  • 16
  • 69
  • 117
  • I'm not sure exactly why you want to have the property on the `Route` but Ember does not (easily) give you access to the route from the controller. `Route`s can acces `Controller`s but there is no default way to go the other way around. – tikotzky Sep 16 '14 at 14:08
  • I know *what* I want to do, but this is my first ember project so I don't entirely know *how* to do it. All I need is to get access to a route parameter from the controller so I can load a different model and merge them together into the output. I can't see any way to get hold of the route parameters in the controller directly, so I thought creating a property on the route would work. – jbrown Sep 16 '14 at 14:14
  • 1
    If you need to load a different model then you should transition to the route passing the new model id and let the router fetch the new model and pass it to the controller – tikotzky Sep 16 '14 at 14:31
  • I need to merge the values of two models into the same output template, not view a different model on its own. The other model doesn't have a route because it doesn't make sense to view it on its own. – jbrown Sep 16 '14 at 14:46
  • if the other model is not based off the route, then in the route how do you know which other model to load? – tikotzky Sep 16 '14 at 14:51
  • 1
    See the second answer: http://stackoverflow.com/questions/20521967/emberjs-how-to-load-multiple-models-on-the-same-route/20523407#20523407, setupController is hit each time the model changes, not just the first time. – Kingpin2k Sep 16 '14 at 14:51
  • @Kingpin2k OK thanks, that looks like what I need. I'll try again with setupController. – jbrown Sep 16 '14 at 14:56
  • 1
    You may also want to think of using another deeper nested resource, if you want the url to be changing with your id. It sounds like you want a master detail view – Kingpin2k Sep 16 '14 at 14:57
  • TBH I'm inheriting where I should be composing, but one step at a time. – jbrown Sep 16 '14 at 15:01

0 Answers0