mardi 14 février 2017

Async relationship not present after logout (canonical state vs current state)

In an Ember application we have a quite typical asynchronous relationship between two entities:

const Event = DS.Model.extend({
    user: belongsTo('user', {async: true});
});

const User = DS.Model.extend({
    events: hasMany('event', {async: true});
});

Although this code is just an example, for the reasons I'll expose now I think it's fair to assume that everything is working properly here.

During the phase of startup of the webapp and just after login, the server sends to the client-side a configuration object that sets some initial state. The entities included in this initial object are inserted manually into the Ember Data store as:

store.pushPayload('event', data.setup.my_entity);

To force the relationship to be executed, after the login and for the sake of this exercise one of my component does:

this.myUser.get('events').then(events => {
    debugger;
});

Under normal conditions, this works perfectly. You could have a 'events.forEach' and iterate through all the events available. You'll see in events a class that holds the events and the pieces that I have on the UI expecting to have this info available are rendered properly. Among others, two variables appear inside the 'event' variable. If we assume that we only have one event we would see:

  • canonicalState: Array[1] (InternalModel inside)
  • currentState: Array[1] (InternalModel inside)

When users log out, among other things we clear the store:

this.store.clear();

I've used the Ember inspector tool with Chrome, and all the entities are removed. The problem appears now. If we log in again, the same code to download the configuration object is executed and entities are pushed to the store as shown before.

If again we have a single event, with a different identifier, we'll see the following state in the debugger breakpoint specified before:

  • canonicalState: Array[1] (InternalModel inside)
  • currentState: Array[0]

And all the expected behavior just does not work. If you had a 'events.forEach' nothing would happen.

In my understanding, the fact that the relationship does work at the beginning proves that the relationship is well written and the payload coming from the server is correct.

After logging out and logging in, all the executed code is identical as when you do it for the first time. Payloads seem to almost identical (just a new and valid identifier could be returned for the events collection, but that is expected).

It seems as if clearing the store was leaving there some state that is not really cleaned up, and honestly, I don't understand what is going on. I've tried the unloadAdd method with no luck either.

Thank you so much,

Aucun commentaire:

Enregistrer un commentaire