Wednesday, March 25, 2020

Small Framework for Server-Side Calls in Aura

Promises in Aura

In Aura, we spend a lot of time writing the same method over and over again: calling the server. Now, we can include a small script that calls the server and uses Promises to promote small, reusable code. This one method means that we only have to write about three lines of code to call the server, including error handling. This requires minimal adaptation, but it should make it far easier to write server calls in a consistent manner.

The Static Resource

We simply need a small piece of code that we will load in our components.

aura.js

window.server = function({component, method, params}) { return new Promise( $A.getCallback((resolve, reject) => serverSync({component:component, method:method, params:params,
success:resolve,
error:reject})) ); } window.serverSync = function({component, method, params, success, error}) { var action = component.get(method); params && action.setParams(params); action.setCallback(this, result => { switch (result.getState()) { case "DRAFT": case "SUCCESS": success.call(this, result.getReturnValue()); break; default: error.call(this, result.getError()); } }); $A.enqueueAction(action); }
These two simple functions allows us to write small code for all of our components. We can add further features later, but that is outside the scope of this post. For now, we are simply interested in calling the server as efficiently as possible.

Using in a Component or App

With this small script, we now need to write our components a little differently. Instead of handling the aura:valueInit handler like we used to, we now need to wait for our simple script to load. It is small and loads nearly instantly, so there should not be any visible lag time. Here is what a component should start off with now:
<aura:component controller="staticDemo"> <ltng:require scripts="{!$Resource.aura}" afterScriptsLoaded="{!c.init}" />

Calling the Server

Now that we have loaded our script, we can now call the server in several different ways.

Calling Once

In the most usual case, we can call the server with just one method and get the result.
server({component: component, method: "c.myMethod", params: {key: value}}) .then(result => component.set("v.someAttribute", result)) .catch(error => helper.displayError(error));

Calling Multiple at Once

Similarly, we can call more than one method at a time, and continue when we're done:
Promise.all([ server({component: component, method: "c.method1"})
.then(result => component.set("v.value1", result)), server({component: component, method: "c.method2"})
.then(result => component.set("v.value2", result)), server({component: component, method: "c.method3"})
.then(result => component.set("v.value3", result)) ]) .then(results => {}) .catch(error => helper.displayError(error));

Calling Multiple in Serial

Sometimes, we need the result of a previous value to call the next step. We can do this with a Promise chain:
server({component:component, method:"c.method1"}) .then(result => (component.set("v.value1", result),
server({component:component, method:"c.method2", params: {someValue: result}}))) .then(result => component.set("v.value2", result)) .catch(error => helper.displayError(error));

Supporting Cacheable Methods

Cacheable (a.k.a. Storable) methods may end up calling the resolve/reject method more than once. This is not compatible with Promises. If you find yourself in a situation where you need to call those methods, use the serverSync method. While it is not actually a synchronous method, it bears a different name to remind you that you need to pass in your own success and error handlers, and that you cannot chain them with promises. This is still arguably more legible than writing the same fifteen or so lines of code every time.
serverSync({component:component, method:"c.serverAction",
success: result => component.set("v.value1", result),
error: error => helper.displayError(error)});

The Commas

You might notice the use of (param) => (operation1, operation2) in the examples above. This design is used to take advantage of the comma operator. I feel that this is marginally more legible than using code blocks ({}). With the comma operator, the function on the left is evaluated and the results are discarded, then the function on the right is called. This allows us to chain Promises together while storing the intermediate results back to our component. Do not forget the parentheses, as they are necessary to make sure the comma operator works as we want to.

Conclusion

I found this pattern to be immensely useful, and I wanted to share it with others, as well as document this for my future self, should I ever forget to use it.

25 comments:

  1. Thanks a lot for explaining practically.
    Really Appreciable!
    Admin Guide to Export Field Permissions for Permission Sets using BOFC Application.

    ReplyDelete
  2. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective.
    Bulk Field Creation

    ReplyDelete
  3. Hire Freelance Salesforce Architect to overhaul your customer journeys with new technology and business apps. Our Salesforce Architect helps you by identifying business and technical goals while enabling you execute on these plans. Get genuine guidance on deploying apps and other customizations while minimizing architectural complexity.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Nice article please do visit my website for salesforce training

    ReplyDelete
  6. I read over your blog, and I found it inquisitive. Also, you may also find about Freelance Salesforce Experts

    ReplyDelete
  7. Replies
    1. You got any pills to help him with his sexual abuse charges on kids and dogs?๐Ÿ˜‚

      Delete
  8. Hi Brian,

    I dont know how to send Kudos to you in the salesforce stackexchange community.Every time when i search for something definetly there is a best answer from you.

    Really thank you and i hope you can contribute more and more to the salesforce community.

    Thank you
    Rama Injety

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. How’s prison going๐Ÿ˜‚ Ive heard prison does a lot to people but dang, you went from an overweight black man with a beer gut to looking like an underweight scraggly Arab. MY CHILDREN cackle when they see the photo we got of you in that holding cell, they ask quite frequently to see how bad you are doing, their laughing at your self earned punishment is very much a part of their healing therapy. May you have every day, be the day you truly deserve for your sexual abuse on those 3 kids and the dog. Maybe you should have gotten some mental help for your extreme perversions and abusive personality disorder, you have lied to all of these people for years and been getting their money, all your coding buddies and clients gave you plenty of donations on your begging platforms, should have used it to fix the real problems you had instead of getting booze and video games. You destroyed three children’s safety, trust and childhood. I intend to use my free time to ensure your nomenclature never sees another second of gratification inducing fame again. YOU ARE A CHILD AND DOG RAPIST. In truth, none of that was my doing, you went straight south when you got with Kian after I divorced you. You started abusing them heavily when they didnt want to approve of your personal choice in a hateful, disabled, lap hoping tramp. If I was so bad and horrible, why does everyone in my new family believe I am truly a wonderful addition to their home. My mother in law visits daily to spend time with me and I have lost count how many times she has thanked me for coming into her sons life to give him a family.

    Brian Matthew Fear, birthdate March 17, 1981. It’s now public record, his extreme abuse charge on the dog is for raping it. He pleaded guilty for the child sex abuse charge to have it reduced from 3 kids to 2…. But it makes no difference in karmic tally, neither does the child’s gender to him. Boy or girl alike, can get raped by Brian Fear at a sleepover.

    ReplyDelete