How do I stop ember data from automatically querying all the has many relationships


#1

I have a application which communicates over websockets. Provided that the client has a sufficiently fast internet connection upon login I would like to just push out all the meta data the client will ever need, which should both make the client and the user happy.

However as long as I maintain relationships within my model, ember data tries to query all the relationships, which just means another useless request over the wire. This is true if I set async: true on the model. If I set async: false on the model, ember complains that it expects the data to already be there, but it obviously isn’t.

The reason for that seems to be that I have a group model, which has many items. So the response from the backend looks something like this:

{
  name: 'a group',
  items: ['id1', 'id2']
}

in my group.hbs I display the number of available items:

<div>{{t Items}}: {{items.length}}</div>

Basically ember data should have all the data it needs, but it doesn’t seem to be happy as long as it can’t fetch all the related entities, in case they are referenced somehow.

So, how do I get ember data to just be a little more patient and less chatty with my backend, while still using hasMany relationships?


#2

When you are using items.length ember data populates the DS.PromiseArray items by default. You can avoid this behavior “cheating” and inspecting the core object structure instead:

core{
    name: 'group'
    items: ['id1', 'id2']
}

should generate an object where the items array is stored in _data.items (not 100% sure, check it). Of course if you change it and make the model dirty, the location changes (inflightattributes ?) so I’d create a method in the model, somthing like:

core{
    ....
    itemsLength: function(){
        if this.get('isdirty)
            ....
        else
            ....
    }.property()

Not the best solution probably, but it should work…