The right using of Lightning helpers

Eugene Vabishchevich 06/12/2018. 4 answers, 292 views
lightning-components lightning

Could someone explain what is the correct way of using Lightning helpers? Is that any place where I can find best practices how to move code from the controller to the helper? Sometimes I see in js controller methods that call methods directly from the helpers without any additional logic inside of controller method. But sometimes developers move part of code to the helpers.

4 Answers


codeyinthecloud 06/12/2018.
  • Use Controllers to listen to user events and other events like component, application events. But delegate business logic to helper.
  • Do similar delegation in all Renderer functions (render, rerender and so on).
  • Anytime you need to call one controller function from another controller function, move that logic to Helper.

Here is a article to give you a very good idea about helper.js vs controller.js.

Understanding JavaScript Controllers Versus Helpers In Lightning Components


tsalb 06/12/2018.

The most important thing is that same instances of the component share one helper. This is important when you're doing things like action.setStorable() in your helper and you have same data that should only be fetched once, then all instances of your component (except the first) won't go to server to fetch it.

Generally, it's best to handleClick\handleChange\handleLoad in your controller and then execute/fetch/get it in your helper. This is the pattern that made the most sense to me so far.


Aidan 06/15/2018.

The accepted answer from Rajesh is good here. To re-enforce that and add some non-SF terminology to it, it's helpful to think in terms of the Model-View-Controller pattern.

If you try to draw boxes around a Lightning Component to decide what's the Model, what's the View, and what's the Controller, then I think it ends up like this:

View: The markup

Controller: The controller.js, this translates front-end commands (e.g. click, hover, etc.) into more abstract commands that can be sent to the Model

Model: The helper.js, and the Apex Controller. This is the business logic, storage, etc. It doesn't really know about the UI. The UI should be able to change without this changing. So, for example, if you had a text input in your markup and you changed it for some radio buttons, the model should never have to change. In Lightning, controller.js might have to change but helper.js should not. (Unfortunately, Lightning's abstractions are currently a bit leaky here so you might have to use Apex to get your list of options. You could do this by introducing a new component which just fetches those options, gets included in the markup, and doesn't break MVC in this particular component)

It's easy to get confused about MVC:

  1. Lightning uses "controller" in it's terminology and it doesn't correspond to the C in MVC
  2. There doesn't have to be a 1:1 correlation between boxes in your framework (Lightning) and boxes in the MVC diagram e.g. the Model is implemented by more than one thing in Lightning
  3. It exists at multiple levels at the same time. <lightning:input> is MVC, and so is your custom component that uses it
  4. Everyone's heard of MVC, so it's assumed that everyone understands it. But, it's worth thinking a bit more about it. Have a watch of this from Uncle Bob Martin: https://youtu.be/o_TH-Y78tt4?t=27m30s

Keith C 06/12/2018.

Anything other than trivial logic ends up being broken up into methods that call other methods and the lack of that capability in controllers (but it being allowed in helpers) makes controllers often end up delegating all their work to a corresponding helper method.

I sure hope there is some strong benefit of this controller/helper pattern to the framework or some categories of components because I've found it in general to just result in more boiler plate code to have to write and refactor - not elegant.


HighResolutionMusic.com - Download Hi-Res Songs

1 BLACKPINK

Kiss And Make Up flac

BLACKPINK. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
2 Martin Garrix

Waiting For Tomorrow flac

Martin Garrix. 2018. Writer: Pierce Fulton;Mike Shinoda;Martijn Garritsen;Brad Delson.
3 Martin Garrix

Access flac

Martin Garrix. 2018. Writer: Martin Garrix.
4 John Legend

Written In The Stars flac

John Legend. 2018. Writer: Kiana Brown;Santoy;Kevin White;Mike Woods;MZMC;The Heavy Group;Rice N' Peas.
5 Martin Garrix

Yottabyte flac

Martin Garrix. 2018. Writer: Martin Garrix.
6 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
7 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
8 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
9 Post Malone

Sunflower flac

Post Malone. 2018. Writer: Louis Bell;Billy Walsh;Carter Lang;Swae Lee;Post Malone.
10 Dyro

Latency flac

Dyro. 2018. Writer: Martin Garrix;Dyro.
11 Zara Larsson

Ruin My Life flac

Zara Larsson. 2018. Writer: Delacey;Michael Pollack;Stefan Johnson;Jordan Johnson;Sermstyle;Jackson Foote.
12 ZAYN

Fingers flac

ZAYN. 2018. Writer: Zayn Malik;Alex Oriet;David Phelan.
13 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
14 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
15 Sia

I'm Still Here flac

Sia. 2018. Writer: Sia.
16 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
17 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
18 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
19 Blinders

Breach (Walk Alone) flac

Blinders. 2018. Writer: Dewain Whitmore;Ilsey Juber;Blinders;Martin Garrix.
20 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora.

Related questions

Hot questions

Language

Popular Tags