Select your Language

Lightning Introduction 2

AuraEnabled

1. @auraEnabled annotation provides supports for Apex methods and properties to be used with the lightning component Framework
2. Only static @auraEnabled apex methods can be called from client side controller
3. We can wright N number of auraEnabled methods in the class.
4. Pure Vf page(no controller or apex class ) is on a User Context but controller class is on System context
    same as pure lightning component obey user context then in the class we can use With Sharing or without sharing concept
5.In Vf controller method have only void and PageReference return type and no any parameter but in lightning component class it have any return type and any parameter(not support PageReference)
6. AuraEnabled can be variables or can be a methods
========================================================================
Example:1

Public class abc
{
@auraEnabled
public static string show()
{
string name='hello componet';
return name;
}
}
========================================================================
Define variable
@auraEnabled public dataType variable name {set;get;}

========================================================================
Lightning component with apex class

1. With the help of controller we can access apex class in the component

 <aura:component controller="className">
</aura:component>

2. For calling the server side controller we call firstly client side controller because server side controller we can not call directly
3.Client side controller call server side controller
========================================================================
How to invoke server side controller from client side controller

1. Component.get("c.methodName");
2 .setParams({key1:value.key2:value});
If you want to pass the parameter to server side controller , then they should be passed in the form of map (key,value) pair.

var a= component.get("c.methodName");
a.setparams({"Name":"Ankit","Age":28});

3. Create a callback action
 .setCallback(param1,param2)

a. param1:
Param1 value is always "this". It is referring the current instance of component

b. Param2:
It is callback handler.
This will specify weather action is success of fail.
How to handle the action
Function(Response){}
c. We get the state of the response
getState() :New,Running,Success,Error,Incomplete,Aborted.
getReturnValue(): This will return the value returned by the method invoked from the server.
========================================================================
Example:

var abc=componet.get("c.methodname");
abc.setParams({"Name":"Ankit","Age":28});
abc.setCallback(this,function(response){
                                                                   var state=response.getSate();
                                                                    if(state==='sucess')
                                                                    {
                                                                          var result=response.getReturnValue();
                                                                      }
                                                                  });

4. EnqueueAction
      a. Any call from JavaScript to Apex class will be Asynchronous call
      b. This run this operation as asynchronous , add this action in to queue
       $A.enqueueAction(abc);
========================================================================

Example:

Client-side-controller

({
                        show: function(component,event,helper)
                      {
                        var abc=component.get("c.callme");
                        abc.setcallback(this,function(response){
                                                                                          var state=response.getState();
                                                                                           if(state==='sucess')
                                                                                               {
                                                                                            var result=response.getReturnValue();
                                                                                           component.set("v.result",result);
                                                                                            console.log("Result:"+result):
                                                                                                }
                                                                                          else
                                                                                             {
                                                                                                 console.log('State failed');
                                                                                              }
                                                                                      });
                          $A.enqueueAction(abc);
                       }
})

Component:

<aura:component controller="Example1" >
        <aura:attribute name="result" type="string" />
        <lightnign:button label="Submit " onclick="{!c.show}" />
         Result: {!result}
</aura:component>

Server-side-controller

public class Example1
{
@auraEnabled
public static string callMe()
{
      string name = ' hi i am from apex class';
      return name;
}
}

========================================================================



No comments:

Post a Comment