All the docs I've read mention how to access data thru public static methods annotated with @AuraEnabled but is it possible to access public variables defined in the server controller like we can in visualforce pages?
1 Answer
Actually, neither Visualforce nor Lightning components allows access from the page/component context to variables in an Apex controller.
Lightning Components allows access purely through methods exposed as at least public, and annotated as @AuraEnabled
. Remember that LCF has no feature that is directly executed on the server side. So if you want to surface it, you need to write a method.
Visualforce gives several options:
- Getter/setter methods
- Properties
- RemoteAction annotated methods
But technically speaking, none of these are variables in the strictest sense.
I suspect the confusion comes from the fact that properties might look like variables syntactically, but the way I think of properties is as a special syntactic convention for creating getter and setter methods (although there may be more to it than that).
In making this distinction, I primarily rely on the difference that variables store some kind of value, and methods contain one or more instructions to execute logic.
-
To confirm I understand you correctly, I am interpreting your response as there is no way to persist data across requests on the server side through lightning. Commented Jul 27, 2015 at 18:40
-
If by server-side persistence you mean as a variable on the instance, then no, you cannot persist the values as the controllers are stateless. You can persist data by storing it in a record, but I suspect that's not what you're trying to do. Can you describe your use case in more detail, and why you think you need server side state? Commented Jul 27, 2015 at 22:22
-
The need comes from the current implementation of our application / business logic. The application state is maintained server-side which we read / update on subsequent request. But based on what I am reading on this thread, it seems converting our application to Lightning would require a sizable refactoring of our classes and maintain application state on the client instead. Commented Jul 27, 2015 at 22:33
-
So this is about stateful and view state? As Skip states, there is no such thing in Lightning Components. So yes, you would need to implement this yourself. This could either be on the client side (cache, session storage, or whatever), or the server side (storing data into your own custom state object).– pchittumCommented Jul 28, 2015 at 8:33
-
FYI @GeraldTruong I would recommend you update your question to reflect that this is about accessing View State, and not just about variable access.– pchittumCommented Jul 28, 2015 at 8:34