In this article, I will tell you the best practices for Lightning Components, Lightning component bundle and Events.
a. Developer Productivity
b. Feature availablity
c. Application Scaliblity
a. Application Complexity
b. Time to Delivery
c. Overall Cost
Lightning Component Bundle
- Use Controllers to listen to user events and other events like Component Event, Appliction Event.
- Delegate your business logic to helper methods.
- Do not trigger DML operation on component initializaton. If you are doing DML in init(), you are creating a CSRF(Cross-Site Request Forgery).
- Do not modify DOM in Controller. If you modify DOM in Controller it will call renderer method which will end in no result.
Always write your business logic in helper functions because
- Whenever a component runs Lightning Framework creates an instance of the Controller, an instance of the Renderer for each component but creates only one copy of the Helper and passes the reference of the Helper into every Controller instance and every Renderer instance. Below picture will make you understand this well.
Since Helper is shared across everything, it allows us to share and keep logic across of Controllers and Renderers in one place. It also helps us keep logic within Controllers and Renderers lean. Anytime you need to call one controller function from another controller function, move that logic to Helper.
- Use Renderer whenever you want to customize default rendering, rerendering, afterrendering and unrendering behaviour for a component.
- Do not fire an event in renderer, firing an event in a renderer can cause an infinite rendering loop.
- If you need to directly manipulate DOM elements in your component, you should do it in the component’s renderer.
- Always use events to implement communication between components.
- Always try to use a component event instead of an application event, if possible. Component events can only be handled by components above them in the containment hierarchy so their usage is more localized to the components that need to know about them. Below picture will make you understand better about component event.
- Application events are best used for something that should be handled at the application level, such as navigating to a specific record.
- It’s a good practice to handle low-level events, such as a click, in your event handler and refire them as higher-level events, such as an approvalChange event or whatever is appropriate for your business logic.
- If you have a large number of handler component instances listening for an event, it may be better to identify a dispatcher component to listen for the event. The dispatcher component can perform some logic to decide which component instances should receive further information and fire another component or application event targeted at those component instances.
- Do not use onclick and ontouchend events in a component. The framework translates touch-tap events into clicks and activates any onclick handlers that are present.
That’s it for now I will come with more details in upcoming posts. 🙂