4

So I'm having trouble finding the correct pattern to solve this problem. I have a custom lightning component, with an apex controller assigned to it. There's an apex method, controllerInit, which I need to execute before the component renders. However, controllerInit is not static, so I can't call it from the component's js controller, which means I can't call it on init via <aura:handler>. On a VF page, I'd simply set the action attribute where I set the controller, but that doesn't seem to be an option in lightning components.

So how do I manage this? Is it possible to call a non-static apex method on the initialization of a lightning component?

16

You can't directly instantiate an Apex Code controller, because view state isn't a thing in Lightning. Anything you want to save between round trips needs to be passed back to the client.

Either pass the information back and forth between the client and server, or use static initializer blocks. Note that static initializers will run every time the server is called, so that may result in performance issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.