I have an Apex class with two constructors. When I call an @AuraEnabled method from the helper, it does not hit any of the constructors. How can I specify a constructor from the helper?
Thanks
I have an Apex class with two constructors. When I call an @AuraEnabled method from the helper, it does not hit any of the constructors. How can I specify a constructor from the helper?
Thanks
Best Answer
Apex server-side controllers for Lightning components use methods that are declared
static
and@AuraEnabled
. Sincestatic
methods are not bound to an object instance, the constructor is never called during a server call from Lightning.static
methods don't get to maintain state between calls in instance variables. All of your state should be kept client-side, in your JavaScript controller or component attributes. Any initialization that must be done server-side has to be done in astatic
context, not in a constructor, unless the constructor is for some object that is itself instantiated within thestatic
method.Here's an example.