0

I have an Ember App with Models for:

  • Food
  • Meals

(there are other Models but these are ommitted for the sake of brevity).

The router is configured thus:

this.resource('food', { path: '/food/:food_id' });

this.resource('meals',function() {
  this.resource('meal', { path: '/:meal_id'}, function() {
    this.route('edit');
  });

  this.route('new');
});

In my Food template I need access to the list of Meals in order to print them. Initially I thought of the needs property on the FoodController would do the trick.

App.FoodController = Ember.ObjectController.extend({
    needs: ['meals', 'mealsIndex'],
    ...
});

In my template I then tried

{{#each meal in controllers.meals.content}}
    TEST
{{/each}}

...but nothing is displayed. I've actually tried several other methods but nothing is having any effect.

I suspect that perhaps the the MealsController's Model isn't being set until the meals route is entered.

I'm totally stuck here and the docs aren't helping. Any assistance much appreciated.

getdave
  • 208
  • 2
  • 8

1 Answers1

0

You'll need to request the models from the server manually, like this:

App.FoodRoute = Ember.Route.extend({
    model:function(params){
        var store = this.store;
        return Ember.RSVP.hash({
            food:store.find('food',params.food_id),
            meals:store.find('meals')
        });
    },
    controllerSetup:function(controller,models) {
       controller.set('model',models.food);
       this.controllerFor('meals').set('model',models.meals);
    }
});

When your app enters the Food route, it'll request both the current food and all the meals. Then it'll set the model for the food controller and all the meals as the model on the meal controller. You should be able to access them in your template as you have set up using needs.

NicholasJohn16
  • 2,390
  • 2
  • 21
  • 45
  • Thank you very much for a clear and concise answer. Another option I've literally just worked out is to request the Meals (from the store) in the Application Route and then set the "content" property on the Meals Controller using `controllerFor`. I initially tried doing the above directly in the Food.Route but that did not work. I will test your solution now. – getdave Apr 25 '14 at 17:09
  • When trying your solution at the moment I'm getting an error `Error: undefined is not a function`. Please note that Food does not implement a Ember Data Adapter and thus doesn't use the store. Not sure how to debug this. – getdave Apr 25 '14 at 17:27
  • Try changing the food line in the RSVP.hash to however you're fetching the food item. Do you know which line is causing the issue? – NicholasJohn16 Apr 25 '14 at 20:36