More flexible async relationship


#1

Customizable query for async requests

I have a model:

Floor = DS.Model.extend({
  tables: DS.hasMany('table', {async: true})
});

When I query tables I would really like to send a single GET request with floor_id as a parameter instead of multiple requests for each table with table_id as a param.

For now I use an intermediate TableSet object as a work around. It works nicely but requires extra effort to keep it in sync when tables get added/removed.

It would be very helpful if I can specify the way I query async relationship.

probably something like this:

Floor = DS.Model.extend({
  tables: DS.hasMany('table', {async: true, query: 'tablesQuery'}),
  
  tablesQuery: function(){
    return this.get('store').find('table', { floor_id: this.get('id') } );
  }

});

Check if async relationship is loaded.

There is currently no way I can find out whether async relationship is actually loaded. Here is an example how I would I use the isLoaded flag:

Floor = DS.Model.extend({
  auditSet: DS.belongsTo('auditSet', {async: true}),

  auditSetChangedOnServer: function() {
    if this.get('auditSet.isLoaded') {
      this.get('auditSet').then(function(as){ return as.reload() });
    }  
  }
});


#2

Im not sure what adapter you are using, but the REST or ActiveModelAdapter there is a property called coalesceFindRequests. It will collect all your GET calls into one call, but it will still be based upon the table IDs, not the floor ID. http://emberjs.com/api/data/classes/DS.RESTAdapter.html#property_coalesceFindRequests

The other option you can do is use the ‘Links’ hash. Essentially instead of passing down IDs, your passing down where to find the information that ember needs. Check it out here http://andycrum.com/2014/06/02/getting-started-with-ember-data/ (just look for ‘links hash’)