vendredi 8 avril 2016

Multiple hasMany relationships of the same type on a model

I'm working with a class which has multiple hasMany relationships of the same type, but with different attribute names. For example:

export default DS.Model.extend({
  sunday: DS.hasMany('scheduled-time'),
  monday: DS.hasMany('scheduled-time'),
  tuesday: DS.hasMany('scheduled-time'),
  wednesday: DS.hasMany('scheduled-time'),
  thursday: DS.hasMany('scheduled-time'),
  friday: DS.hasMany('scheduled-time'),
  saturday: DS.hasMany('scheduled-time'),
  ...

This might represent something like, "on Monday, employee X is scheduled to work 9am-12pm (one scheduled-time instance), and again from 1pm-5pm (another scheduled-time instance)." For each day of the week, an employee could have zero, one, or many scheduled times.

The other side of the relationship might look like this:

export default DS.Model.extend({
  start_time: DS.attr('string'),
  end_time: DS.attr('string'),
  schedule: DS.belongsTo('schedule'),
  ...

However, Ember data doesn't seem to allow this, and says that I need to explicitly state the inverse of the belongsTo (as detailed here: http://ift.tt/1N0mYTe). Here's where I think my data model is broken: I can't think of any logical way to say that a given scheduled-time belongs to any given day of the week. Is this data model broken? I have a feeling that instead of organizing all of the scheduled-times under individual days of the week, they should really belong collectively under a single attribute, and perhaps the days of the week would be computed properties which did some filtering instead.

Is this type of relationship possible, or should I do some refactoring?

Aucun commentaire:

Enregistrer un commentaire