function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Armoury05Armoury05 

Lightning Component Best practice - Client Side vs Server side controllers

Hello,

Would like to get some basic guidance on when to use client side controllers and when to use server side controllers. For example - lets say there is a form and I am collecting input data into to insert into an Account object. And lets say I have to set some default values for few fields along with it. So it is better to set this value in client side controller or handle this in apex?
Also please do share any other links/docs on the best practises in general. Thanks
Best Answer chosen by Armoury05
Narender Singh(Nads)Narender Singh(Nads)
Hi,
The concept of Lightning Component development is based on reducing server calls and to only modify/update that part of component which needs updation/modification. That being said, coming to your question, we should try to code in such a way that our application/component should only make a server side when necessary i.e when we need to access the database of saleforce.

And for the example you mentioned, in your form it's better to set default values at the client side and then add a validation at server side controller(the latter is not necessary, depends on the scenario).

Links:
https://balkishankachawa.wordpress.com/tag/lightning-best-practices/
https://www.slideshare.net/developerforce/secure-salesforce-lightning-components-best-practices

In the end, it comes down to the more you code, more you learn. :D

Please mark my answer as best if it helps you so that others with similar question can benefit from posts.
Any doubts, do let me know.

Regards,
Narender

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,
The concept of Lightning Component development is based on reducing server calls and to only modify/update that part of component which needs updation/modification. That being said, coming to your question, we should try to code in such a way that our application/component should only make a server side when necessary i.e when we need to access the database of saleforce.

And for the example you mentioned, in your form it's better to set default values at the client side and then add a validation at server side controller(the latter is not necessary, depends on the scenario).

Links:
https://balkishankachawa.wordpress.com/tag/lightning-best-practices/
https://www.slideshare.net/developerforce/secure-salesforce-lightning-components-best-practices

In the end, it comes down to the more you code, more you learn. :D

Please mark my answer as best if it helps you so that others with similar question can benefit from posts.
Any doubts, do let me know.

Regards,
Narender
This was selected as the best answer
Naveen KNNaveen KN
@Narendar, small question. Considering the default values are no need to show to the user, why do we need to set at the client side? we can set the field values and do its validation at the server side itself right?

To add to the point, in the latest lightning release (Summer' 18), we have a better client side validation implementations, to avoid extra server call for validating any objects data.
Narender Singh(Nads)Narender Singh(Nads)
Hi Naveen,
The reason for providing those default values is so that you can implement client side validations in your appication. It is a good practice to have both client side and server side validations in your application. It reduces the unnecesarry submissions and server calls.
Armoury05Armoury05
@Naveen - Can you share the Summer 18 validation improvements link which you highlighted. Did a quick glance in the release notes but could not find any
Armoury05Armoury05

@Narender - Thanks. that was elaborative. Qn : In the form example your suggestion was to set the default values on the client side for an object. on this approach - Lets say i have the following setup.

1. Form with Title, First Name, Last Name input fields.
2. Attribute of sobject type - Account. And is bounded to the above three input fields.
3. Now lets assume that the Account -> Phone field i have to default it to '12345'.

Now as per my understanding :
1. Setting the 'Phone' value in the client side controller will re-render the component once again as it alters a view variable.(which is account obj) which is not ideal i guess. is that correct?
 

Narender Singh(Nads)Narender Singh(Nads)
Hi,
Firstly, you are not setting the defaults in the Client side controller, you are setting the default at the client side.
And to answer your query, you will not be re-rendering any part of your component setting the 'Phone' value in the client side.
You will be setting the default for phone like this:
<aura:attribute name="acc" type="Account" default="{'sobjectType':'Account', 'Phone':'12345'}"/>
Deepika1007Deepika1007
Hi,
Please find Lightning component best practices below.
http://deepikamatam.blogspot.com/2018/06/salesforce-lightning-component-best.html