You need to sign in to do that
Don't have an account?
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
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
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
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
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.
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.
@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?
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'}"/>
Please find Lightning component best practices below.
http://deepikamatam.blogspot.com/2018/06/salesforce-lightning-component-best.html