The service Lightning Component to write a clear asynchronous JavaScript code
Lax is a Lightning Component to write a clear asynchronous JavaScript code. It helps you to remove the boilerplate code in Lightning Components. Lax combines capabilities to efficiently call Apex with powerful exception handling and list of utility techniques, giving you an ability to write asynchronous code in a modern approach.
lax
gets the context of consumer component$A.getCallback()
lax
in consumer's component aura:valueInit event handlerClick on the button below to deploy the component to the org
Define lax
component in a custom component markup:
<!-- ContactsComponent.cmp -->
<aura:component controller="LaxExamplesController">
<!-- Define lax component and pass consumer's component object as a context attribute (required) -->
<c:lax context="{!this}" />
<aura:attribute name="records" type="Contact[]" access="private"/>
<aura:handler name="init" action="{!c.onInit}" value="{!this}" />
</aura:component>
Enqueue an action in component's aura:valueInit event handler function to get initial data:
// ContactsComponentController.js
({
onInit: function (component, event, helper) {
// equeue getContacts server-side action and set the callback
component.lax.enqueue('c.getContacts').then(contacts => {
// $A.getCallback is not required. lax does it automatically
component.set('v.records', contacts);
});
}
});
lax
automatically defines a property on the consumer's component (context) objectlax
is ready to use in consumer's component aura:valueInit event handlerlax
object. Every lax
object inherits methods from grand parent
Navigate to component API Reference documentation to see full list of available functions and capabilities
Actions can be enqueued by passing the relevant parameters and options.
lax.enqueue(name[, params[, options]]).then(callback)
- call action with parameters, options and simple callbackcomponent.lax.enqueue('c.getContact', { id: recordId }, { background: true })
.then(contact => {
component.set('v.record', contact);
});
component
- A reference from a list of parameters that every client-side controller function hasparams
- An object with list of parameters (optional)
options
- An object with list of options that can be applied to the action (optional):
background
- to sent action separately from any foreground actions
abortable
- to make action potentially abortable while it's queued to be sent to the server
storable
- to quickly show action cached data from client-side storage without waiting for a server trip.
It is not recommended to use with Promise API by Salesforce. Use lax.action instead.enqueue()
- The function returns LaxPromise object which inherited from Promise. It overrides .then()
and .catch()
function to automatically wrapper callback using $A.getCallback()
.lax.enqueue(name[, params[, options]]).then(callback).catch(callback)
- handle errors thrown by the servercomponent.lax.enqueue('c.save', { record: record })
.then(id => {
component.set('v.record.id', id);
})
.catch(errors => {
console.error(errors);
});
lax.enqueue(name[, params[, options]]).then(callback).then(callback)
- performing multiple chained actionscomponent.lax.enqueue('c.getParentValue')
.then(parentValue => {
component.set('v.parentValue', parentValue);
return component.lax.enqueue('c.getDependentValue', { parentValue: parentValue });
})
.then(dependentValue => {
component.set('v.dependentValue', dependentValue);
});
lax.enqueueAll(actions).then(callback)
- performing multiple concurrent actionscomponent.lax.enqueueAll([
// { name : '...', params: {...}, options: {...} }
{ name: 'c.getContacts' },
{ name: 'c.getAccounts' },
{ name: 'c.getOpportunities' }
])
.then(results => {
// results: [ [contacts], [accounts], [opportunities] ]
const contacts = results[0];
const accounts = results[1];
const opportunities = results[2];
});
actions
- The array of actions to enqueue concurrently:
name
- the name of an actionparams
- an object with list of parameters (optional)
options
- an object with list of options that can be applied to the action (optional)
results
- The list of values returned from enqueued actionslax.action(name)
- create and return LaxAction buildercomponent.lax
.action('c.getContact')
.setStorable()
.setParams({ id: recordId })
.setThen(contact => {
component.set('v.record', contact)
})
.setCatch(error => {
console.error(error);
})
.enqueue();
LaxAction
is an object with the list of functions to construct a server-side actionLaxAction
does not use Promise API
LaxAction
builder approachLaxAction
:
setParams(params)
- set an object with list of parameterssetThen(callback)
- set success callback functionsetCatch(callback)
- set failure callback function to handler server-side errorsenqueue()
- enqueue constructed action. The only function LaxAction
that return undefined
setStorable()
- set an action as a Storable
setBackground()
- set an action as a Background
lax.lds(id)
- create and return LaxDataService to execute Lightning Data Service (LDS) actions based on Promise APIcomponent.lax.lds('serviceAuraId');
lax.lds(id).getNewRecord(sobjectType[, recordTypeId[, skipCache]])
- the function to load a record template to the LDS targetRecord
attributecomponent.lax.lds('contactRecordCreator')
.getNewRecord('Contact')
.then(() => {
const rec = component.get("v.newContact");
const error = component.get("v.newContactError");
if (error || (rec === null)) {
console.log("Error initializing record template: " + error);
}
});
lax.lds(id).saveRecord()
- the function to save the record that loaded to LDS in the edit EDIT
modecomponent.lax.lds('recordHandler')
.saveRecord()
.then(result => {
// handle "SUCCESS" or "DRAFT" state
})
.error(e => {
// handle "ERROR" state
})
.incomplete(e => {
// handle "INCOMPLETE" state
})
lax.lds(id).deleteRecord()
- the function to delete a record using LDScomponent.lax.lds('recordHandler')
.deleteRecord()
.then(result => {
// handle "SUCCESS" or "DRAFT" state
})
.catch(e => {
// handle "ERROR" or "INCOMPLETE" state.
// or you can use divided handlers: .error(), .incomplete()
})
lax.event(eventName)
- creates an object with LaxEventBuilder
prototype and the context event by provided name// Fire Component Event:
component.lax.event('sampleComponentEvent')
.setParams({payload: { type: 'COMPONENT'} })
.fire();
// Fire Application Event:
component.lax.event('e.c:AppEvent')
.setParams({payload: { type: 'APPLICATION'} })
.fire();
lax.createComponent(type[, attributes])
- creates a component from a type and a set of attributescomponent.lax.createComponent('aura:text', { value: 'Single Component Creation' })
.then(result => {
// result has a property "component" with created component
single.set('v.body', result.component);
})
.catch(e => {
// handle "ERROR" or "INCOMPLETE" state
});
lax.createComponent(type[, attributes])
- creates an array of components from a list of types and attributeslax.createComponents([
['aura:text', { value: 'Multiple Component Creation #1'}],
['aura:text', { value: 'Multiple Component Creation #2'}],
['aura:text', { value: 'Multiple Component Creation #3'}]
])
.then(result => {
// result has a property "components" with list of created components
multiple.set('v.body', result.components);
})
.incomplete(e => {
// handle "INCOMPLETE" state
})
.error(e => {
// handle "ERROR" state
});