lundi 5 juillet 2021

Share common relationships between Ember (Octane) models

In our Rails backend, we have a lot of Concerns server side. For example, several models import an AppointableConcern which allows the resources to be associated with Appointments.

In our Ember Octane models, we use inheritance instead, and I'm not sure whether that's a good idea. For example, we have these two models...

// models/groupable.js
import { belongsTo } from '@ember-data/model';
import ApplicationModel from './application'

export default ApplicationModel.extend({
  group: belongsTo('group', { inverse: 'groupables' }),
});

// models/appointable.js
import { hasMany } from '@ember-data/model';
import Groupable from './groupable';

export default Groupable.extend({
  appointments: hasMany('appointment')
});

... as well as some models extending either Appointable or Groupable.

To me it seems inheritance has been abused to share logic between classes that are not closely related: An Appointable is not a specific implementation of a Groupable (multiple Groupables can form a Group) at all.

Now I wonder:

  • Is this a recommended/standard way to share logic between models?
  • Is there a more flexible alternative? Considering my example above, directly using group: belongsTo('group', { inverse: 'groupables' }) in any model that should be groupable and appointments: hasMany('appointment') in all models that appointments should be allowed to associate with seems like a simple solution.



Aucun commentaire:

Enregistrer un commentaire