Skip to content

Instantly share code, notes, and snippets.

@sohalloran
Created November 7, 2018 17:48
Show Gist options
  • Select an option

  • Save sohalloran/1c5d9881626c648fc9574ea635d88c42 to your computer and use it in GitHub Desktop.

Select an option

Save sohalloran/1c5d9881626c648fc9574ea635d88c42 to your computer and use it in GitHub Desktop.
<aura:component implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes" extensible="true">
<aura:method name="execApex" action="{! c.execApex }">
<aura:attribute name="callingComponent" type="Aura.Component" default="{! this }" />
<aura:attribute name="method" type="String" />
<aura:attribute name="params" type="Map" />
</aura:method>
{! v.body }
</aura:component>
<?xml version="1.0" encoding="UTF-8"?>
<AuraDefinitionBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>43.0</apiVersion>
<description>Library contains methods to work with server side (APEX).</description>
</AuraDefinitionBundle>
({
execApex: function(cmd, event, helper) {
return helper.execApex(event.getParam('arguments'));
},
})
({
execApex: function (eventParams) {
var d = new Date;
var log = console.log.bind(console, eventParams.callingComponent ? eventParams.callingComponent.getName() : '');
log(`${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`, 'start exec');
return new Promise($A.getCallback((resolve, reject) => {
log(`${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`, 'start promise');
let { callingComponent, method, params } = eventParams;
let action = callingComponent.get('c.' + method);
params && action.setParams(params);
action.setCallback(this, function (response) {
if ('SUCCESS' !== response.getState()) {
console.error(response.getState(), response.getError());
log(`${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`, 'reject');
reject(response);
return;
}
log(`${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`, 'resolve');
resolve(response.getReturnValue());
});
$A.enqueueAction(action);
log(`${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`, 'enqueueAction');
}));
},
})
@sohalloran
Copy link
Author

sohalloran commented Nov 7, 2018

Usage:

<c:AjaxLib aura:id="AjaxLib" />


init : function (component) {
    component.find('AjaxLib').execApex(component, "doAction", { 
                     paramId : component.get("v.recordId"), 
                 }).then(result => { 
                         console.log(result);
                 }).catch(result => {
		console.log(result.message);
                 });
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment