An Engineer in Atlanta

Ember and limiting nested items

November 06, 2014

One issue I have been facing lately is I have a model, let’s call it Post that then has a lot of child objects, like Comments. So Post hasMany Comments. But by default when you set these up in a model, setting async to true, if you’re using Ember Data it will call every single id listed in the comments array. I’ve been trying to get around that so I can implement paging if I need to.

For instance, say you have a url like /posts/1/comments. Without a way to limit comments, by default it would load all of them at once. While trying to solve this I wound up in the source code and stumbled across the piece I needed. It was here. The key was the findHasMany method was looking for JSON from the server like this:

  "post": {
    "id": 1,
    "title": "Rails is omakase",
    "links": { "comments": "/posts/1/comments" }

So instead of the normal comments array, it is replaced by a links object that specifies the url to hit for that resource. This in turn is just an index action that I can limit to however many records I need for the first pull. This doesn’t get me to paging yet, but it’s a start.

Dan Smith

Written by Dan Smith who lives and works in Atlanta doing random stuff. You should follow him on Twitter