2

I am trying to have handle multiple controllers at one route in Ember. This response seems like the way to go, but am having difficult getting this to work. Here is a simple example that fails to work:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    myData = [1,2,3];

    return Em.RSVP.hash({
      docs1: myData,
      docs2: myData
    });    
  },
  setupController: function(controller, model) {
    controller.set('model', model.docs1);    
    this.controllerFor('documents').set('model', model.docs2);
  }
});

App.DocumentsController = Ember.ArrayController.extend({
  count: function() {
    return this.get('length');
  }.property('@each'),  
});

App.IndexController = Em.ArrayController.extend({  
  count: function() {
    return this.get('length');
  }.property('@each'),  
});

And a JSBin showing the results:

http://jsbin.com/wavigada/1/edit

You can see that the IndexController reports the correct count of 3, but the DocumentsController doesn't get set.

Can anyone help me out?

Community
  • 1
  • 1
dvreed77
  • 1,890
  • 1
  • 22
  • 36
  • The question you linked to discusses multiple models, not controllers. I honestly don't know if multiple controllers for one route is even possible. – GJK Feb 27 '14 at 22:53
  • In your template where you use the `render` helper if you don't pass an explicit model then Ember.js will return the singleton instance of the documents controller which will have the model set appropriately. Here's the working example: http://jsbin.com/wavigada/5/edit. (Also, you have some trailing commas you should clean up after your computed properties. – Sarus Feb 28 '14 at 00:04
  • @Sarus Thank you, it seems your answer is what I was looking for. Frustrating it was soo simple@ – dvreed77 Feb 28 '14 at 02:13

1 Answers1

0

You will need to include the documents controller whose content you populate in setupController in your IndexController via needs:

App.IndexController = Em.ArrayController.extend({  
  needs: ['documents'],
  count: function() {
    return this.get('length');
  }.property('@each'),  
});

Then you need to change your template to:

<script type="text/x-handlebars" data-template-name="index">
  {{count}}
  {{render 'documents' controllers.documents}}
</script>

Note that just putting {{render 'documents' controllers.documents}} (as you did in your question) refers to a documents property of your current model, which doesn't exists.

See: http://jsbin.com/wavigada/6/

chopper
  • 6,179
  • 6
  • 31
  • 53