Lightning Data Service

What is Lightning Data Service(LDS)?
-Lightning Data Service is the data layer for Lightning. We can load, create, edit or delete a particular record by LDS without calling Server/Apex Class. You can compare Standard Visualforce Controller to Lightning Data Service.

PROS

1. LDS supports sharing rules and field level security.
2. Record loaded by LDS is cached and shared across all components.
3. Minimize XMLHttpRequests.
4. After record changes, notification process is available. You can compare with publish-subscriber model.
5. It is offline supported.

CONS

1. LDS is applicable to operate on a particular or single record at once. LDS is not supported for multiple records at once.
2. LDS is only applicable in Lightning Experience. It is not supported in Lightning Components calling from Visualforce page or Lightning Out. If this Visualforce Page is added into Lightning Experience or in Salesforce Mobile app, then LDS is supported.

Syntax


<force:recordData aura:id="accountInfoId" 
                  recordId="{!v.recordId}" 
                  mode="EDIT" layoutType="FULL" 
                  fields="Id, Name, Phone" 
                  targetRecord="{!v.record}" 
                  targetFields="{!v.simpleRecord}" 
                  targetError="{!v.errors}" 
                  recordUpdated="{!c.updateRecord}" />

A. force:recordData tag is to define Lightning Data Service.
B. force:recordData has some parameters to fulfill the Lightning Data Service. We will discuss one by one.

1. recordId: It defines the Record Id for which LDS will run.
2. mode: It can be “EDIT” or “VIEW“. It will tell the mode of operation.
3. layoutType: It will specify the Layout such as “FULL” or “COMPACT“.
4. fields: LDS will query for these fields internally.
5. fields or layoutType is required to query data by LDS.
6. targetRecord: It is populated with the loaded record.
7. targetFields: It is populated with the simplified view of the loaded record.
8. targetError: It is responsible to show the errors, if errors are occurred during operation.
9. recordUpdated: It is responsible to do certain operation if anything changes to the loaded record. Remember, for only this operation, we are calling method in javascript”{!c.updateRecord}”.

In the next articles, we will see how to load, create, edit or delete record using Lightning Data Service.

4,073 total views, 2 views today

Rating: 4.5/5. From 6 votes.
Please wait...

You may also like...