Lightning Apps – Difference Between Controller And Helper?

I have just recently started building Lightning Applications, and I do not understand why there are two Javascript components in each bundle. What is the difference between the controller and the helper? How do I know what code to put where?

Answer

In addition to what everyone else has said…

The helper is designed to have shared code in it. So, you could have code shared by your renderer and your controller or by multiple controller functions. That should go in the helper JS file.

I think that a good practice is to let the controller respond to events, possibly perform some amount of control flow, and delegate business logic to the helper as a separation of concerns. It also sets you up to be able to reuse your business logic in other parts of the component in the future.

Helper code can be shared among components when they are related through inheritance. If one component extends another it inherits its super component’s helper and can override it.

Sub components can call super component controller methods, but that may be deprecated in the future according to the documentation in the Lightning Components Developer’s Guide.

Components can communicate with each other via events to “share” code. For example, if you have a component that handles writing business logic errors in a uniform way in your app, and handles some sort of error related event, you could fire an event to trigger it to handle an error.

Attribution
Source : Link , Question Author : Adrian Larson , Answer Author : Peter Knolle

Leave a Comment