i0n
i0n

Reputation: 926

Counting nested association objects in ember.js

I am trying to return counts for associated objects in my ember controller.

I have:

App.Comment = DS.Model.extend({
    discussion: DS.belongsTo('App.Discussion')
});

App.Discussion = DS.Model.extend({
    meeting: DS.belongsTo('App.Meeting'),
    comments: DS.hasMany('App.Comment')
});

App.Meeting = DS.Model.extend({
    discussions: DS.hasMany('App.Discussion')
});

Now in my Meeting controller I want to return counts for the discussions and comments associated with that meeting:

App.MeetingController = Ember.ObjectController.extend({
    discussionCount: function(){
        return this.get('discussions.length');
    }.property('discussions')
});

I can get discussions fine, but I can't find a way to get the associated comments of the discussions associated with each meeting. Any ideas how it is best to do this?

Upvotes: 1

Views: 596

Answers (1)

Cyril Fluck
Cyril Fluck

Reputation: 1581

I don't know how many discussions and comments you have, but this is typical of the n+1 select problem (see What is SELECT N+1? ).

There is a real risk for performance issue.

If you don't think it's a problem, you can create a filter on the itemController to get all the comments associated to a meeting, and then use this array to compute the total count.

Otherwise you can cache the number of comments on a discussion server-side and add this counter to the App.Discussion model.

Upvotes: 1

Related Questions