0
votes

In Ember if I have a model that is a list of users, if in the UsersController I do:

users = this.get('model');

users.map(function(user) {
  user.name
});

should it not resolve the promise and return the user records? I'm confused on why this is not working for me, or how to get the model data the correct way. Thank you in advance.

1
Would you mind giving some more code, the route, and controller. - Kingpin2k
For context, the code you are showing works perfectly fine, but it's hard to gauge if your context is valid... emberjs.jsbin.com/OxIDiVU/806/edit - Kingpin2k

1 Answers

2
votes

The model promise is resolved by the router. Ember, by default, sets the controller's content property as the route's model unless you override the route's setupController() method. Your issue lies in the formatting of the map function.

It seems like you're using an array controller, because the model is an array, so do the following:

App.UsersController = Em.ArrayController.extend({
  users: function() {
    return this.map(function(user) {
      return user.get('name');
    });
  }.property('@each.user'),
});

You can make this code even more streamlined by using Em.Array's mapBy() method, as follows:

App.UsersController = Em.ArrayController.extend({
  users: function() {
    return this.mapBy('name');
  }.property('@each.user'),
});

If you're using the list of users in your template you can do this easily with a {{#each users}} helper. However, if you're using this list for other properties in the controller, be sure to to use the right observer to watch for items being added to the array:

someOtherProperty: function() {
  var users = this.get('users');
  // Do stuff with users array here...
}.observes('users.[]')

See setting up a route's model and setting up a controller if you're unfamiliar with how the models stuff works.