mardi 20 octobre 2015

How to use Ember data / JSONAPI to represent a Map of objects

I have a data model that includes a response that has a map of answers, and each answer is associated with a question. Response, Answer, and Question are all mapped entities. The Response object might look something like:

response: {
  id: 4
  date: 2015-4-6
  answers: {
    doesSmoke: { 
      id: 349, 
      value: 'no',
      question: {
        id: 700,
        title: 'Do you smoke?'
      }
    },
    favoriteSport: { 
      id: 32, 
      value: 'football',
      question: {
        id: 701,
        title: 'What's your favorite sport?'
      }
    }
  }
}

Because there are 300+ answer names, and they change all the time, I don't want to name them all in my model. I also don't want to just use DS.attr('') for "answers", because I want them to be represented as proper answer objects so that I can update them. So how can I have it as a map with keynames, so in my templates I can still say {{response.answers.doesSmoke.value}} or {{response.answers.[doesSmoke].value}} without having to name all the possible properties individually?

Apologies if I'm missing something obvious in the Ember docs -- the only way I can think of doing this now is to create an answers: DS.attr('') property, and then manually push separately loaded answer objects into it.




Aucun commentaire:

Enregistrer un commentaire