Issue with using `this` inside `afterLoad` static method
Problem
The afterLoad method is confusingly not called with a this context of the controller constructor but is instead undefined.
TypeScript is advising that this will be the controller constructor but at runtime it is failing.
Steps to reproduce
Create a controller
export class LoaderController extends Controller { static values = { durationSeconds: { type: Number, default: 30 }, }; static afterLoad(identifier: string, application: Application) { const { controllerAttribute } = application.schema; console.log('afterLoad test', this); // this is undefined console.log('afterLoad stuff', {identifier, controllerAttribute }); console.assert(this.values.durationSeconds.default, 30); // errors as this is undefined } }
Expect that the this will be the controller constructor. Instead it is undefined.
Potential solution
| const afterLoad = (definition.controllerConstructor as any).afterLoad |
const afterLoad = (definition.controllerConstructor as any).afterLoad?.bind(controllerConstructor)