Question

I have two objects User and Post. A user has many posts and a post belongs to a user.

How do I, within the user controller, use findBy or another method to get to a featured post with the posts array??

Here is how I implemented the UserController; however, the featuredPost computed property is coming back as undefined. Is this best approach? If so, what am I missing?

App.User = DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  client: DS.belongsTo('App.Client', { async: true }),
  posts: DS.hasMany('App.Post', { async: true })
});

App.Post = DS.Model.extend({
  client: DS.belongsTo('App.Client', { async: true }),
  user: DS.belongsTo('App.User', { async: true }),
  title: DS.attr('string'),
  body: DS.attr('string'),
  isFeatured: DS.attr('boolean')
});

App.UserController = Ember.ObjectController.extend({
  needs: ['post'],
  posts: (function() {
    return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
      content: this.get('content.posts')
    });
  }).property('content.posts'),

  featuredPost: (function() {
    return this.get('content.posts').findBy('isFeatured', true)
  }).property('content.featuredPost'),
});
Was it helpful?

Solution

Take a look at this: http://emberjs.com/api/#method_computed_filterBy

App.UserController = Ember.ObjectController.extend({
  featuredPost: Ember.computed.filterBy('posts', 'isFeatured', true)
});

Also, in

featuredPost: (function() {
  return this.get('content.posts').findBy('isFeatured', true);
}).property('content.featuredPost')//should be observing 'posts'

You are basically observing content.featuredPost but from what youve mentioned that property doesnt exist, the property you should be observing is 'posts'. This is a mistake that i made when i was learning ember too, so felt like pointing out. Also using content is optional, you can directly observe the model associated with controller.

Also From doc, findBy seems to return just first item that matches the passed value, not all of them. So to get first match it should be

App.UserController = Ember.ObjectController.extend({
  featuredPost: function() {
    return this.get('posts').findBy('isFeatured', true);
  }.property('posts')//assuming user model hasMany relation to posts
});

Also I would go with the latest version of ember data and make following changes:

App.User = DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  client: DS.belongsTo('client', { async: true }),
  posts: DS.hasMany('post', { async: true })
});

App.Post = DS.Model.extend({
  client: DS.belongsTo('client', { async: true }),
  user: DS.belongsTo('user', { async: true }),
  title: DS.attr('string'),
  body: DS.attr('string'),
  isFeatured: DS.attr('boolean')
});

This would be good read : https://github.com/emberjs/data/blob/master/TRANSITION.md

And here is a bare minimum working example : http://jsbin.com/disimilu/5/edit

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top