Giter VIP home page Giter VIP logo

action.apex's Introduction

Action.apex

Action.apex is a tiny library that removes the pain points from Salesforce Lightning server controllers.

Why Action.apex?

Action.apex aims to solve some pitfalls in Lightning server controllers. First of all is that you cannot receive custom types in your @AuraEnabled method arguments. Instead, you receive strings and deserialize them, which is ugly. Then you have to surround every @AuraEnabled method with try-catch block to return the AuraHandledException, so that the front end can show anything except the Internal Server Error. Action.apex comes with solutions to get rid of these harassment from your Lightning server controllers.

Dependencies

Action.apex has a dependency over R.apex.

Please include it before including Action.apex.

Preliminary Knowledge

You don't really need any knowledge on R.apex before you can move on with Action.apex. Knowledge on R.apex will be a plus, though.

Getting Started

Actions

In Action.apex, an important step is to put your server controller logic into objects called Actions. Actions represent standalone remote actions with meta information like the name, parameters and return types. Here is how we define a custom action:

public class MessageDTO {
    public String content;
}

public class CustomAction extends Action {
    public CustomAction() {
        super('echo'); // Define the unqiue name of the action

        // Define parameter information
        param('msg', MessageDTO.class, 'The input message');
        // Auto convert to map as MessageDTO does not have AuraEnabled fields
        returnRaw();
    }

    public override Object execAction(Object arg) {
        MessageDTO msg = (MessageDTO)arg;

        return msg;
    }
}

This is a very simple implementation that echoes whatever it receives a MessageDTO.

We can define multiple parameters, and correspondingly we need to implement execAction methods as below:

Method Description
execAction() Execute action with no arguments
execAction(Object) Execute action with one argument
execAction(Object, Object) Execute action with two arguments
execAction(Object, Object, Object) Execute action with three arguments
execActionN(List<Object>) Execute action with more than 3 arguments

Action Registry

The next step is to register the action.

Action.Registry registry = new Action.Registry();
registry.action(new CustomAction());

Invoke Server Controllers

Then we can set up our action registry in Lightning server controller method.

@AuraEnabled
public static Object invoke(String name, Map<String, Object> args) {
    return registry.invoke(name, args);
}

So now we can call the server action in this way.

var action = cmp.get("c.invoke");
action.setParams({
    name: 'echo',
    args: {
        input: {
            content: 'message',
        },
    },
});

action.setCallback(this, function(response) {
    // ...
});

$A.enqueueAction(action);

And the whole flow is ready.

What Action.apex did

In this example, behind the scenes, Action.apex helps us to:

  • Convert the input parameters to MessageDTO
  • Catch any exceptions thrown and rethrow it as AuraHandledExceptions
  • Auto convert MessageDTO into map objects so that it can be serialized

action.apex's People

Contributors

liumiaowilson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.