Lightning Apex Class
Lightning Apex Class
Apex class :
-------------
1. If you want to invoke any apex class in the in the lightning component
then the class should have @AuraEnabled methods or variables in it.
2. How to define a apex class
public with sharing class ClassName{
@AuraEnabled methods
@AuraEnabled variables
}
3. if you want to invoke any apex method in the lightning component then
a.Method should be annotated with @AuraEnabled
b.Method should be public /global
c.Method should be static
d.Method can return any type of value
e.Method can have any type of parameters and any no of parameters
f. syntax :
@AuraEnabled
public static Integer callMe(){
return 10;
}
@AuraEnabled
public static Account callMe(String key){
return [select Name,Phone from Account where name:key limit
1];
}
<!--
@AuraEnabled
public static List<Account> callMe(String key){
return [select Name,Phone from Account where name:key ];
}
-->
4. if you want to invoke any variable directly in the component
a. variable should be public
b. varaible should have @Auraenabled
c. It can have any datatype
Example :
@AuraEnabled public String name {set;get;}
@AuraEnabled public Integer age {set;get;}
-->
7. If we want to define more than one apex class in the lightning component how
we define
Example 1:
public class Example extends Demo{
}
<!-- <aura:component controller="Example" > -->
Example 2;
<!--
one.cmp
<aura:component controller="Demo" >
</aura:component>
two.cmp
<aura:component extends="c:one" controller="Example">
</aura:component>
-->
Step 1:
var abc =component.get("c.apexMethodName");
a. This will invoke the method from the apex class and run the
operation
b. once the server-side operation is completed then it will call
callbackaction
Step 2: callback action
abc.setCallback(this,function(response){
logic
});
a. response : what ever the value returened by the apex class
this will stored as response
1. getState() : This will return the state of the action
a. SUCCESS
b. ERROR
c. NEW
4. INCOMPLETE
e. ABORTED
f. RUNNING
2. getReturnValue() :
a. if the apex class method has any return type ,value
returened by the method
will given by this
$A.enqueue(abc);
</apex:page>