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

belongsTo working in client local store but are not update on another client #367

Open
scandinave opened this issue Feb 13, 2016 · 3 comments

Comments

@scandinave
Copy link

Hi, maybe it's related to this #364. I don't know.

So I have two model with a belongsTo relationship like this :

/* message.js */
import DS from 'ember-data';

export default DS.Model.extend({
  content: DS.attr('string'),
  conversation: DS.belongsTo('conversation', {async:true}),
  owner: DS.belongsTo('user', {async:true})
});

and

/* user.js /*
import DS from 'ember-data';
export default DS.Model.extend({
    email : DS.attr('string'),
    name: DS.attr('string'),
    timestamp: DS.attr('number'),
    conversations : DS.hasMany('conversation', {async:true})
});

In my template i test this case like this :

{{#each model.conversation.messages as |message|}}
    {{app-message message=message currentUser=session.uid owner=message.owner.id}}
{{/each}}

When i add a message to the conversation, all works fine. But if i reload the page or connect as another user , i see the new message but message.owner.id is null.

This is the code that save the message

send : function(conversation) {
    var that = this;
    var controller = this.controllerFor('conversation');
    this.store.findRecord('user', that.get('session').get('uid')).then(function(user) {
        var message = that.store.createRecord('message', {
            content  : controller.get('message'),
            conversation : conversation,
            owner : user
        });
        conversation.get('messages').pushObject(message);
        message.save().then(function() {
            conversation.save();
        });
    });     
}

Any idea?

@tstirrat
Copy link
Contributor

Do you have better luck if you set the inverse to null in messages.owner?:

owner: DS.belongsTo('user', { async:true, inverse: null })

I think ember-data may be overwriting the message.owner shortly after loading the message. Sometimes when ember-data thinks there is an inverse relationship (in your case user.messages), it sees that it is empty and deletes the other side of your relationship. A similar thing was happening in the emberfire demo app.

It is annoying, and it seems some change in ember-data recently has caused this to happen more often.

Note: I am guessing at this point, setting the inverse to null will help us confirm if something similar is happening here.

@tstirrat
Copy link
Contributor

Sorry, didn't mean to close.

@scandinave
Copy link
Author

No better luck. Problem is still here. If I leave the conversation and comes back again, message.owner is filled. The problem appears just on the page reload.

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

2 participants