Search code examples
javascriptextjsassociationsstoreextjs6

ExtJS store is not updated or wrongly updated after changing associated object


I'm totally new to ExtJS and have found an issue in an existing application.

I have two models: A and B. Defined like this

Ext.define('A', {
  extend: 'Ext.data.Store',
  fields: [
    { name: 'id', type: 'string' },
    { name: 'title', type: 'string' },
  ]
});

Ext.define('B', {
  extend: 'Ext.data.Store',
  fields: [
    { name: 'id', type: 'string' },
    { name: 'name', type: 'string' },
    {
      name: 'a',
      reference: {
        type: 'A',
        inverse: 'bs',
      },
    },
  ]
});

So each A got a field bs, which contain associated instances of B. Also, each B has a field a.

In the ViewModel I have

// ...
stores: {
  as: {
    model: 'A',
    autoLoad: true,
    session: true,
    remoteSort: false,
    listeners: {
      load: 'renderSomething',
    },
  },
  bs: {
    model: 'B',
    autoLoad: true,
    session: true,
  },
}
// ...

And finally, in the view I render each A as a tab, and its Bs as its content. Also, I have a combobox regarding B, which upon selection should reassign particular B to another A. And that works fine, but like, once, max twice. After one, or two changes it seems like either As are not updated or are with some cache. What's more, B is always getting assigned to a new A, but not removed from the previous one, so I end up with same B in more than one A. After refreshing the page all looks as it should.

The B is reassigned to another A via combobox, which value is bound to currentB.a and on Save click the currentB.save() method is being called. In the callback of save method I call this.getViewModel().getStore('as').load();, should that update the bs field of A records?


Solution

  • Well, after some time of trying different things I came up with other approach. Instead of loading/reloading whole store I am manually updating the record using set method of it after saving the value using session. Seems like it is solving the issue.