0
votes

there is already a couple of questions asking for similar thing, but none of the answers works for me so far. I have following code, where I want to access model from UsersController in DashboardController:

JP.DashboardController =  Ember.ArrayController.extend({
  needs: ['users']
});

JP.UsersController =  Ember.ArrayController.extend({
      model: function(){
        return JP.User.find();
    },
    sortProperties: ['id']
});

My dashboard.template looks like this:

<div class="row-fluid">
  <div class="span2">
    {{#if controllers.users.isLoaded}}
            {{#each user in controllers.users }}
                {{user.name}}
            {{/each}}
    {{else}}
            Users not loaded
    {{/if}}
  </div>
  <div class="span10">
      {{ outlet }}
  </div>
</div>

Why are users never loaded? What is wrong with my code? Thanks

1

1 Answers

0
votes

The model function in your UsersController is the cause of the problem. Always assign a real object (or object array) to the model/content property of your controller. You should do the data fetching in the Route instead of the Controller. I guess you also have a UsersRoute, if you have a UsersController? This could work:

JP.UsersRoute = Ember.Route.extend({
  setupController : function(controller){
   // the controller param is the instance of UsersController
   controller.set("model",JP.User.find());
  }
});
JP.UsersController =  Ember.ArrayController.extend({
    sortProperties: ['id']
});

If you have no UsersRoute and just a DashboardRoute, try this:

JP.DashboardRoute = Ember.Route.extend({
  setupController : function(controller){
   // the controller param is the instance of DashboardController, so we have to get the UsersController first
   this.controller("users").set("model",JP.User.find());
  }
});