4

I need to use a couple of variables in my lightning components. Hence, I declared them with @AuraEnabled annotation. There are a couple of methods which also being used in a lightning controller(i.e. they are also with @AuraEnabled annotation).

The variables which are annotated with @AuraEnabled, will be updating in different conditions of methods. Hence I need to these variables in that @AuraEnabled methods. However, I am getting compile time error, with following approaches:

  1. If I declare @AuraEnabled variables, and directly using them in @AuraEnabled methods; I am getting variable not declared error.

e.g.

@AuraEnabled
public Integer someCount;

@AuraEnabled
public static List<custom_object> processData(){
   if(this){ 
       someCount += someValue;
   }else{
       someCount -= someValue;
   }
}
  1. If I use this.comeCount += someValue; , I am getting Variable does not exist: this.sum1 error.

Please let me know if there is any way to achieve this or am I doing something wrong.

4
  • You can access them in apex just like they were not auraenabled. It has no effect in apex. Your issue is you are trying to access a local class property from a static method.
    – Eric
    Commented Apr 30, 2017 at 19:20
  • Yes. Actually it is kind of a static variable. A count, which can be increased and decreased in any method. I need that variable as a common variable, so that I will be getting updated value on UI. However, when I try to use this with static, it throws error - @AuraEnabled method must be public or global and cannot be static. is there any workaround to achieve/get updated variable (which will be updated in any of the AuraEnabled method)?
    – Ysr Shk
    Commented Apr 30, 2017 at 19:26
  • All aura enabled must be static. In apex they are no different than any other static property or method.
    – Eric
    Commented Apr 30, 2017 at 19:28
  • I was also expecting the same, However I am getting compile time error.
    – Ysr Shk
    Commented Apr 30, 2017 at 19:29

1 Answer 1

10

Yes, you can access static variables, but there's no point for your specific use case. @AuraEnabled is used to expose custom classes and methods to Lightning. Static variables will be reset between each transaction, just as they are in Visualforce. Note the following:

Don’t store component state in your controller (client-side or server-side). Store it in a component’s attribute instead.

If you need to maintain a counter, do so in your component's code. You must always pass in whatever state you wish to use. For example, you might write something like this:

public class Payload {
    @AuraEnabled public Integer counter;
}
@AuraEnabled public static Payload someMethod(Payload state) {
    if(...) {
        state.counter++;
    } else {
        state.counter--;
    }
    return state;
}

And in your component:

doSomething: function(component, event, helper) {
    var action = component.get("c.someMethod");
    action.setParams({ state: component.get("v.state"); });
    action.setCallback(function(result) {
        // Not included, but remember to check isValid/result status
        component.set("v.state", result.getReturnValue());
    });
    $A.enqueueAction(action);
}
2
  • Thanks for the workaround! However, please correct me if I am wrong: "we can't use the variable directly in method. We have to get it through other means."
    – Ysr Shk
    Commented May 1, 2017 at 6:06
  • 1
    @YsrShk yes, that'd be a simple way of saying it. They don't maintain the value between each transaction, and so have to be stored on the client.
    – sfdcfox
    Commented May 1, 2017 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .