Discuss anything related to web development but no technical support questions


Post by dennyferra »

Bryntum Grid (6.1.1) TypeScript definition file says it can take a Promise<boolean> for the beforeRemove event of the grid. However when I make an async handler and return false the item is still removed. Is there a different way to handle this? I would like to run some async code on the remove and then return true/false if the item should be removed.

TypeScript definition:

onBeforeRemove: ((event: { source: Store, records: Model[], parent: Model, isMove: boolean, removingAll: boolean }) => Promise<boolean> | boolean | void) | string

Can reproduce this on the php backend example https://bryntum.com/products/grid/examples/php/ by adding beforeRemove:

grid.store.on({
    beforeRemove  : async () => false,
})

without async it works fine.

Ultimately I'm trying to create a Store that works with tRPC (https://trpc.io/) so I need to customize the add/remove/update/delete events to route the relevant operations through our tRPC api (which is all async and doesn't use the common REST format).

(posting in community since I'm waiting for my company to renew our license)


Post by mats »

Bryntum Grid (6.1.1) TypeScript definition file says it can take a Promise<boolean> for the beforeRemove event of the grid.

Can you please share where you see this? I see no trace of it ever stating it can be async:

 /**
     * Fired before records are removed from this store by the {@link #function-remove} or {@link #function-removeAll}.
     * Also fired when removing a child record in a tree store using {@link Core.data.mixin.TreeNode#function-removeChild}.
     * The remove may be vetoed by returning `false` from a handler.
     * @event beforeRemove
     * @param {Core.data.Store} source This Store
     * @param {Core.data.Model[]} records The records which are to be removed.
     * @param {Core.data.Model} parent The record from which children are being removed when using a tree store. Only
     * provided when removing a single node.
     * @param {Boolean} isMove This flag is `true` if the child node is being removed by
     * {@link Core.data.mixin.TreeNode#function-appendChild appendChild} to be moved
     * _within the same tree_.
     * @param {Boolean} removingAll This flag is `true` if the operation is removing the store's entire data set.
     * @preventable
     */

Post by Animal »

Since remove is not async, you could work around this.

Have your handler always return false at the end of it to veto the removal.

But before that, kick off the asynchronous processing, which, if it finds at the end that the record should be removed can call store.remove(theRecord, true). Passing the silent flag so that it does not then consult your listener again.


Post by dennyferra »

Hi Mats, there is no explicit mention of async/promise in the documentation. I just noticed the Promise<boolean> return type of the event in the TS declaration file located in node_modules\@bryntum\core-thin\core.d.ts (AjaxStore class > onBeforeRemove)

Animal: I think your suggestion will work for me, thanks!


Post by marcio »

Hey dennyferra,

Thanks for letting us know. I created a ticket to fix the type of beforeRemove definition https://github.com/bryntum/support/issues/10244.

Best regards,
Márcio


Post Reply