Tuesday, May 28, 2019

Standard controller of lightning component (force:recordData)

What is Lightning Data Service?

Lightning Data Service (LDS) act as the data layer for Lightning.
If we are not using LDS, every component within our app makes independent calls to the server to perform operations on a record, even if all components within our app are dealing with same record data. These are independent server calls which reduces performance.

LDS identifies and eliminates requests that involve the same record data, sending a single shared data request that updates all relevant components,  it also provides a way to cache data to work offline in case the user gets disconnected, intelligently syncing the data once the connection is restored.

If we have a Lightning application that creates, reads, updates, or deletes records, LDS is the best, most efficient way to do CRUD operations.

So how do we take benefits of Lightning Data Service? 

All we have to do is use one of the following form-based components , or the very versatile force:recordData component.

lightning:recordForm =>   Display, create, or edit records

lightning:recordViewForm => Display records only

lightning:recordEditForm => Create or edit records only

force:recordData => Display, create, edit, or delete records with granular customization

We have seen the use
of Lightning:recordForm,Lightning:recordEditForm,Lightning:recordViewForm in our previous article.

Link to previous post,

The form-based components take care of layout, validation, create record,update record,delete record,edit record changes and error handling.

  So why we need force:recordData?

  force:recordData is known as standard controller of lightning component similar to we have standard controller in visualforce page. On its own, force:recordData does not have inbuilt UI. We need to define the fields for taking inputs. The lack of UI elements makes it a powerful addition. We can use it to create highly customizable user interfaces beyond what the form-based components provide. We can use custom components to display the data fetched by force:recordData.

  Sample syntax:

  <force:recordData aura:id="someId"


recordId specify the record on which processing needs to be done.
mode can be either EDIT or VIEW.(View for displaying records, edit for creating or updating records)
layoutType specifies the layout (FULL or COMPACT).
fields specifies which fields in the record to query.
target attributes are attributes that force:recordData populates itself.
1) targetRecord is populated with the loaded record
2) targetFields is populated with the simplified view of the loaded record
3) targetError is populated with any errors

Method available which we can use in JavaScript controller are:

saveRecord()    // It insert or update the loaded record.
deleteRecord()  // It deletes the loaded record.
getNewRecord()  // It loads a new record template that performs an insert when data is saved.
reloadRecord()  // It reruns the loading code to overwrite the current targetRecord with the current                                   attribute values.

No comments:

Post a Comment