Our pure JavaScript Scheduler component


Post by nate »

On Scheduler Pro 4.0.0

This might be a bug around replacing resources, we're getting an id collision error. But I'm also messaging in the forum to ask if the way we're replacing resources is not good. Is there a better way than just removing and inserting at the same spot?

Minimal Reproduction:

     const resourceStore = this.scheduleEngine.resourceStore;
    const previous = [
      {
        id: '1',
        name: '1',
      },
      {
        id: '2',
        name: '2',
        children: [
          {
            id: '2-1',
            name: '2-1',
          },
        ],
      },
      {
        id: '3',
        name: '3',
      },
    ];

const current = [
  {
    id: '1',
    name: '1',
  },
  {
    id: '2',
    name: '2',
    children: [
      {
        id: '2-1',
        name: '2-1',
        children: [
          {
            id: '2-1-1',
            name: '2-1-1',
          },
        ],
      },
    ],
  },
  {
    id: '3',
    name: '3',
  },
];

resourceStore.add(previous);
for (let i = 0; i < 3; i++) {
  console.log(`removing ${previous[i].id}`);
  resourceStore.remove(previous[i].id);
  console.log(`adding ${current[i].id}`);
  resourceStore.insert(i, current[i]);
}
  }

logs

removing 1
adding 1
removing 2
adding 2
removing 3
adding 3

core.js:4352 ERROR Error: Id collision on 3
    at ResourceStore.register (schedulerpro.module.js:27617)
    at ClassDefEx.joinStore (schedulerpro.module.js:19657)
    at ClassDefEx.joinStore (schedulerpro.module.js:99396)
    at schedulerpro.module.js:16390
    at Array.forEach (<anonymous>)
    at schedulerpro.module.js:16388
    at Array.forEach (<anonymous>)
    at ClassDefEx.internalAppendInsert (schedulerpro.module.js:16386)
    at ClassDefEx.insertChild (schedulerpro.module.js:16271)
    at ResourceStore.insert (schedulerpro.module.js:22145)

Thank in advance


Post by pmiklashevich »

Hello,

Please make sure the store is set to "tree" explicitly
https://www.bryntum.com/docs/scheduler-pro/#SchedulerPro/data/ResourceStore#config-tree

        resourceStore : {
            tree : true

Although store.add works, there might be a case when you might experience some issues with it using together with trees. Therefore we recommend to use tree specific API
https://www.bryntum.com/docs/scheduler-pro/#SchedulerPro/data/ResourceStore#property-rootNode
https://www.bryntum.com/docs/scheduler-pro/#Core/data/mixin/TreeNode#function-appendChild

// resourceStore.add(previous);
resourceStore.rootNode.appendChild(previous);

Best,
Pavel

Pavlo Miklashevych
Sr. Frontend Developer


Post by pmiklashevich »

About the way you're re-adding dataset.

resourceStore.add(previous);
for (let i = 0; i < 3; i++) {
  console.log(`removing ${previous[i].id}`);
  resourceStore.remove(previous[i].id);
  console.log(`adding ${current[i].id}`);
  resourceStore.insert(i, current[i]);
}

Adding/removing resources in a loop affects the performance.
If you need to replace dataset you can use one of the following approaches:

  1. Replace full dataset with a new one
    https://www.bryntum.com/docs/scheduler-pro/#SchedulerPro/data/ResourceStore#config-data

    resourceStore.data = previous;
    resourceStore.data = current;
    
  2. Remove bunch of records at once / Add bunch of records at once.
    https://www.bryntum.com/docs/scheduler-pro/#Core/data/mixin/TreeNode#function-appendChild
    https://www.bryntum.com/docs/scheduler-pro/#Core/data/mixin/TreeNode#function-removeChild
    https://www.bryntum.com/docs/scheduler-pro/#Core/data/mixin/TreeNode#function-insertChild
    All of these functions support an array of record as the first param.

    resourceStore.data = previous;
    const resource = resourceStore.rootNode;
    resource.removeChild(resource.children);
    resource.appendChild(current);
    

    This is applicable not only for rootNode, but any node in the tree

    resourceStore.data = previous;
    const resource = resourceStore.getById('2');
    resource.removeChild(resource.children);
    resource.appendChild(current[1]);
    

Pavlo Miklashevych
Sr. Frontend Developer


Post Reply