Providing Adaptable Context
Summary
- The
adaptableContextproperty in Adaptable Options allows individual context to be passed into AdapTable - This can then be leveraged at runtime as required
The adaptableContext property allows developers to provide application-specific "context".
Note
This object is very similar in intention to the AG Grid Context object
The property is set by developers in the root of Adaptable Options.
It is subsequently passed by AdapTable into the BaseContext object.
Note
BaseContextis the base object supplied to most functions / callbacks in Adaptable Options- It is also in the
xxxEventInfoobject included in all Adaptable Events
This allows developers to provide data or services which they can subsequently leverage elsewhere in AdapTable, e.g. in Custom Predicates or Display Formats.
Hint
This demo illustrates using Adaptable Context by providing a TimeZone Service leveraged by Custom Predicates
adaptableContext
Record<string, any>Custom Context for the Application
Demo
The demo below shows the typical pattern for using adaptableContext:
- Build an application object — here a
currentUserplus a tinyuserServicethat answers permission questions - Put that object on
adaptableContextonce, alongside the rest ofAdaptableOptions - Retrieve it from any AdapTable callback that needs it
Providing Adaptable Context
- The
userService.tsfile exports acurrentUserobject and a tinyuserServicewith acanPerform()permission helper — both are plain TypeScript, with no framework dependencies adaptableOptions.tsthen exposes them throughadaptableContextand reads them back in three different places:- Config-time — the
userNameoption is set fromcurrentUser.nameand is displayed at the top-right of AdapTable - Runtime — the
Editaction button'shiddencallback asks theuserServicewhether the current user is allowed to edit - Runtime — the
Deleteaction button'shiddencallback does the same for deleting
- Config-time — the
- The default user has the
Managerrole, soEditis visible butDeleteis hidden
Try It Out
- Edit
userService.tsand changecurrentUser.roleto'Trader'(both buttons disappear) or'Admin'(both buttons appear) - Add another consumer — for example, a Custom Predicate handler — and access
currentUser/userServiceexactly the same way viaadaptableContext