1

I am trying to use the needs api to get access the content of the PagesController from my IndexController. But it is not working, the only way I have gotten it to work is to add an IndexRoute and load it through the model hook. Is there a way to use controller needs api to access the content of another controller. Here is the JSBIN: http://jsbin.com/gubuvuhi/1/edit

The PagesControllers:

  App.PagesController = Ember.ArrayController.extend({});

The IndexControler:

 App.IndexController = Ember.ArrayController.extend({
  needs: 'pages',

  fetchPages: Em.computed.alias("controllers.pages.content"), 

});

The PagesRoute:

App.PagesRoute = Ember.Route.extend({ 
   model: function(){
     return this.store.find('page');
   }
});

This index_template doesn't render fetchPages:

 <script type="text/x-handlebars" data-template-name='index'>

  <div class="container">
    <b>failed to display using needs api </b>
    <div>
     {{#each page in fetchPages }}
        {{page.navbar}}
     {{/each}}
  </div>

 </script>

But when I add an IndexRoute and change the IndexTemplate to this, it works & renders the right content:

  <script type="text/x-handlebars" data-template-name='index'>

  <div class="container">
    <b>failed to display using needs api </b>
    <div>
     {{#each page in model }}
        {{page.navbar}}
     {{/each}}
  </div>

 </script>

This is the IndexRoute, note that

 App.IndexRoute = Em.Route.extend({
   model: function(){
    return this.store.find('page');
   }
 });

You will see that this.store.find('pages') is repeated in both IndexRoute and PagesRoute, I want to avoid this by using needs api to get PagesController's content from the IndexController. Is there a way to do this. Here is the JSBIN: http://jsbin.com/gubuvuhi/1/edit

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
brg
  • 3,915
  • 8
  • 37
  • 66
  • Needs would be overkill here, I'd just use `all`, http://jsbin.com/gubuvuhi/3/edit – Kingpin2k Aug 02 '14 at 05:55
  • @kingpin2k thanks for your JSBIN and for pointing out an alternate design approach through the other stackoverflow question you pointed that used renderTemplate and named outlets. Though you have stated that using **needs** is an overkill in this instance, but I will still like to know from you, in what situations is using **needs** not an overkill and in those situations how do I access the model of the other controller. A jsbin demonstrating an example of using the **needs** api to access the model of another controller will be helpful. Thanks for your time. – brg Aug 02 '14 at 09:31
  • This is important info: http://stackoverflow.com/questions/24560266/needs-not-waiting-for-data-to-be-returned-before-rendering-template/24562131#24562131 – Kingpin2k Aug 03 '14 at 03:15
  • And here's another quick example: http://emberjs.jsbin.com/yoluru/1/edit – Kingpin2k Aug 03 '14 at 03:17
  • @kingpin2k I read through your answer on using **needs** in the link you added and from your answer to that question, this was the key insight I was missing that made my code not to work: **Using needs as a way of communicating between controllers really only makes sense in an ancestral pattern (aka communicating with your parents, grandparents etc)** – brg Aug 03 '14 at 13:55

0 Answers0