1

Say I have two Ember Data models: competition and participant

My competition model stores the following data: date, first, second and third. The first, second and third properties will all store IDs of the unique competitors who came in first, second and third place.

My participant model stores the following data: name, surname and competition

Participants only compete in one competition, so the participant model doesn't need a hasMany relationship on competition.

How do I model this in Ember Data?

Here's what I thought might work:

// app/models/competition.js

export default DS.Model.extend({
  rev: DS.attr('string'),
  date: DS.attr('date', {defaultValue: function() { return new Date(); }}),
  first: DS.belongsTo('participant'),
  second: DS.belongsTo('participant'),
  third: DS.belongsTo('participant')
});

// app/models/participant.js

export default DS.Model.extend({
  rev: DS.attr('string'),
  name: DS.attr('string'),
  surname: DS.attr('string'),
  competition: DS.belongsTo('competition')
});

The problem is that the competition key on participant.js requires an explicit inverse, and there's no way of knowing which of the three keys on competition it may relate to.

How should I be specifying my inverses? Do both the competition and participant models require explicit inverses? If so, what should they be? Is this kind of data structure even possible in Ember Data? Should I use a different approach altogether?

Please weigh in. I've turned this problem around in my head for hours, and none of my workarounds have succeeded.

2
  • What does your response json look like?
    – yuяi
    Feb 19, 2015 at 2:39
  • There's no JSON to speak of. I'm using PouchDB, so I don't need to serialize anything. When I look in the localstorage database, all belongsTo fields are null. Feb 19, 2015 at 2:59

1 Answer 1

0

I haven't used ember-data polymorphic association myself, but I believe a polymorphic belongsTo association might suit your needs. My answer is adapted from this blog post . Also check this two tutorials: http://www.toptal.com/emberjs/a-thorough-guide-to-ember-data and http://blog.unspace.ca/post/107228551852/luke-galea-on-ember-data-polymorphic-associations

 //app/models/competition.js
 export default DS.Model.extend({
   rev: DS.attr('string'),
   date: DS.attr('date', {defaultValue: function() { return new Date(); }}),
   participant: DS.belongsTo("particpant", {polymorphic: true,   async: true})
  });

 // app/models/participant.js
 export default DS.Model.extend({
   rev: DS.attr('string'),
   name: DS.attr('string'),
   surname: DS.attr('string'),
   competition: DS.belongsTo('competition')
 });

 // app/models/first.js
 import Participant from '../participant';
 export default Participant.extend({
 });

 // app/models/second.js
 import Participant from '../participant';
 export default Participant.extend({
 });
1
  • I'll test this solution over the weekend and let you know if it worked for me. Thanks for providing this suggestion. My current solution was simply to flatten the competitor model and migrate all the keys for all three competitors to the competition model. It's an ugly hack and I hope there's a better way. Feb 24, 2015 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.