emberfire icon indicating copy to clipboard operation
emberfire copied to clipboard

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

Open scandinave opened this issue 9 years ago • 3 comments

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?

scandinave avatar Feb 13 '16 17:02 scandinave

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 avatar Feb 16 '16 22:02 tstirrat

Sorry, didn't mean to close.

tstirrat avatar Feb 16 '16 22:02 tstirrat

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.

scandinave avatar Feb 23 '16 11:02 scandinave