Monday, February 13, 2017

What are Server-Side Controllers in lightning components and how to use server-side controllers?.

What are Server-Side Controllers in lightning components and how to use server-side controllers?.

Server-Side Controller
Create a server-side controller in Apex and use the @AuraEnabled annotation to enable client- and server-side access to the controller method.
Only methods that you have explicitly annotated with @AuraEnabled are exposed. Calling server-side actions aren’t counted against your org’s API limits. However, your server-side controller actions are written in Apex, and as such are subject to all the usual Apex limits.
This Apex controller contains a serverEcho action that prepends a string to the value passed in.
public with sharing class SimpleServerSideController {

    //Use @AuraEnabled to enable client- and server-side access to the method
    public static String serverEcho(String firstName) {
        return ('Hello from the server, ' + firstName);

In addition to using the @AuraEnabled annotation, your Apex controller must follow these requirements.
  •     Methods must be static and marked public or global. Non-static methods aren’t supported.
  •     If a method returns an object, instance methods that retrieve the value of the object’s instance field must be public.

Creating an Apex Server-Side Controller
Use the Developer Console to create an Apex server-side controller.
1.      Open the Developer Console.
2.      Click File | New | Apex Class.
3.      Enter a name for your server-side controller.
4.      Click OK.
5.    Enter a method for each server-side action in the body of the class. 
6.    Click File | Save
7.    Open the component that you want to wire to the new controller class. 
8.    Add a controller system attribute to the <aura:component> tag to wire the 
   component to the controller. For example:

<aura:component controller="SimpleServerSideController" >

Returning Errors from an Apex Server-Side Controller

Create and throw a System.AuraHandledException from your server-side controller to return a custom error message.

Errors happen. Sometimes they’re expected, such as invalid input from a user, or a duplicate record in a database. Sometimes they’re unexpected, such as... Well, if you’ve been programming for any length of time, you know that the range of unexpected errors is nearly infinite.

When your server-side controller code experiences an error, two things can happen. You can catch it there and handle it in Apex. Otherwise, the error is passed back in the controller’s response.

If you handle the error Apex, you again have two ways you can go. You can process the error, perhaps recovering from it, and return a normal response to the client. Or, you can create and throw an AuraHandledException.

The benefit of throwing AuraHandledException, instead of letting a system exception be returned, is that you have a chance to handle the exception more gracefully in your client code. System exceptions have important details stripped out for security purposes, and result in the dreaded “An internal server error has occurred…” message. Nobody likes that. When you use an AuraHandledException you have an opportunity to add some detail back into the response returned to your client-side code. More importantly, you can choose a better message to show your users.

Here’s an example of creating and throwing an AuraHandledException in response to bad input. However, the real benefit of using AuraHandledException comes when you use it in response to a system exception. For example, throw an AuraHandledException in response to catching a DML exception, instead of allowing that to propagate down to your client component code.

public with sharing class SimpleErrorController {     static final List<String> BAD_WORDS = new List<String> {        'bad',        'words',        'here'    }; 
    @AuraEnabled    public static String helloOrThrowAnError(String name) {         // Make sure we're not seeing something naughty        for(String badWordStem : BAD_WORDS) {            if(name.containsIgnoreCase(badWordStem)) {                // How rude! Gracefully return an error...                throw new AuraHandledException('NSFW name detected.');            }        }     
        // No bad word found, so...        return ('Hello ' + name + '!');    } 

Calling a Server-Side Action

Call a server-side controller action from a client-side controller. In the client-side controller, you set a callback, which is called after the server-side action is completed. A server-side action can return any object containing serializable JSON data. A client-side controller is a JavaScript object in object-literal notation containing name-value pairs. Each name corresponds to a client-side action. Its value is the function code associated with the action Let’s say that you want to trigger a server-call from a component. The following component contains a button that’s wired to a client-side controller echo action. ServerSideLightningController contains a method that returns a string passed in from the client-side controller.
Example : Component


Output : 

for reference  :


Post a Comment

| ,