2

I'm stuck with this problem for so long that I think that I'm missing something obvious. Here's simplified model of my case:

There is Patient that has all meds that he/she takes. And there is Medicine that has all patients who takes it.

// Patient model
Yo.Patient = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    meds: DS.hasMany('medicine')
});
// Medicine model
Yo.Medicine = DS.Model.extend({
    title: DS.attr('string'),
    patients: DS.hasMany('patient')
});

I've read official manual and many resources. The last one and closest to my case was this Toptal tutorial. Here's what I do now:

  1. Before saving new patient I'm pushing array of Medicine objects to newPatient.meds.
  2. Then I save newPatient.
  3. After save is done I'm going through array of Medicine objects pushing newPatient to each of them and saving each.

    newPatient.get('meds').pushObjects(meds);
    newPatient.save().then(function (p) {
        for (var i = 0; i < meds.length; i++) {
            meds[i].get('patients').pushObject(p);
            meds[i].save();
        };
    });
    

But result isn't what it meant to be.

  1. Saved patient doesn't have any meds. In POST request to server meds is empty array.
  2. Saved meds each has patient id in patients. But it always has null on zero position of array.

Also there already was similar question on StackOverflow. But solution is not very usable. And answer is 1.5 years old which is eternity in Ember world.

Edit Proposed in comments by Josh RecordArray as I see now cant't be used in my case. Or maybe it can, but I don't know how. I have a form for creating new patient. There's list of meds checkboxes. So I need to push only checked ones to newPatient.meds.

What I do now: I get ids of checked checkboxes (which equals to corresponding object id), then get Medicine instance for each id and put it in array. Then I try to push it in `newPatient.meds'.

Community
  • 1
  • 1
  • Hi Nikita, one thought is you may be using `pushObject` and `save` in a somewhat unusual way. I'm not sure if `meds[i].get('patients').pushObject(p)` will actually do anything. It's more common to see `meds[i].set('patients', )`. – Josh Padnick Sep 24 '14 at 23:29
  • My second thought is that I would approach this by first compiling the list of meds as a `RecordArray` (for example by using `this.store.find('medicine', )`. Then I would use `myPatient.set('meds', )`. Then I would call `serialize()` on my `newPatient` object before saving it to see how Ember intends to serialize it. If you're not seeing the results there, keep using `console.log` until you can narrow down the specific place where something's not happening as you expect. – Josh Padnick Sep 24 '14 at 23:34
  • @JoshPadnick thank you for advice! I'm new to Ember so I'm sure there is many things I do "unusual" (wrong) way. I will try this approach and report back. – Nikita Kasyanov Sep 25 '14 at 09:50

1 Answers1

1

You should use serializer for this issue. Here is answer to your question

Community
  • 1
  • 1
yAnTar
  • 4,269
  • 9
  • 47
  • 73