Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bidirectional hasMany relationship has empty list on the many side #143

Closed
asuraphel opened this issue Oct 10, 2016 · 7 comments
Closed

Comments

@asuraphel
Copy link

asuraphel commented Oct 10, 2016

My models look like:

grn:

export default Model.extend({
    items: hasMany('grnItem', { inverse: 'grn', async: true })
});

grn-item:

export default Model.extend({
    grn: belongsTo('grn')
});

This is how i'm trying to do the save:

        var items = this.get('grnItems');

        var itemsPromises = [];

        var grnRecord = this.store.createRecord( 'grn', this.get('grn'));

        grnRecord.save().then(() => {
          grnRecord.get('items').then(  (grnItems) => {

            for (var i = 0; i < items.length; i++) {
              var grnItemRecord = this.store.createRecord( 'grnItem',items[i]);

              grnItems.pushObject(grnItemRecord);

              itemsPromises.push( grnItemRecord.save());
            }


            Ember.RSVP.all( itemsPromises).then( function() {

                console.log("Resaving the GRN");

                grnRecord.save();
              }
            ).catch(() => console.log('saving grn failed!'));

grn.items seems to be [] no matter how many items I add. What is going on?

@backspace
Copy link
Collaborator

I think you’re missing including the grn when you save a grnItem. From the readme:

      const comment = this.store.createRecord('comment',{
        comment: comment,
        author: author
      });

The child element needs a reference to its parent. In your case, I think you could add this line after you createRecord for the grnItem:

grnItemRecord.set('grn', grnRecord);

You also need to save both ends of the relationship, unfortunately, as also mentioned in the readme. After you save all the items, you need to grnRecord.save() again.

@asuraphel
Copy link
Author

asuraphel commented Oct 10, 2016

You're totally right, @backspace. I changed the code to sth like:

grnRecord.save().then(() => {


            grnRecord.get('items').then( (grnItems) => { 

              for (var i = 0; i < items.length; i++) {
                var grnItemRecord = this.store.createRecord( 'grnItem', items[i]);

                grnItemRecord.set( 'grn', grnRecord);

                grnItems.pushObject(grnItemRecord);

                itemsPromises.push( grnItemRecord.save());
              }


              Ember.RSVP.all( itemsPromises).then( function() {

                  console.log("Resaving the GRN");

                  console.log( grnRecord.save());
                }
              ).catch(() => console.log('saving grn failed!'));


            }
            );

          });

I can't access grn from grnItem on Ember console though! Am i missing sth?

@backspace
Copy link
Collaborator

Are you saying that grnItemRecord.get('grn').then(grn => console.log(grn)) doesn’t show anything?

@asuraphel
Copy link
Author

@backspace that's right. I'm doing sth like $E.store.peekRecord('grnItem', "D91A091A-17FE-BC1F-8ABF-6F2755AA9795").get('grn') after the save is complete!

@backspace
Copy link
Collaborator

hmm, nothing stands out to me apart from the belongsTo not being async, but I think that’s default anyway. It’s hard to say without a live example I can toy around with.

@jlami
Copy link
Collaborator

jlami commented Nov 7, 2016

In ember-pouch async was not default, #159 could fix this. Which would also remove the need for the save on the hasMany side.

@asuraphel
Copy link
Author

The issue went away, as per @backspace's suggestion, by adding a serializer inside app/serializers/application.js whose contents are:

import { Serializer } from 'ember-pouch';

export default Serializer.extend();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants