Upgrade to Pro — share decks privately, control downloads, hide ads and more …

{{should-avoid this.renderModifiers butHow=true}}

{{should-avoid this.renderModifiers butHow=true}}

EmberFest, Paris, Day 1, Sept 22, 2022

The ember-render-modifiers README states that the modifiers are “very useful for quickly bridging the gap between classic components and Glimmer components, but they are still generally an anti-pattern.” It also says that you should “rethink your functionality rather than use these modifiers as a crutch.”.

However, the README doesn’t give any real guidance on what we should do instead.

In this talk I’m explain why these modifiers are problematic and explore the two main use cases I’ve seen ember-render-modifiers used for and how we can go about replacing them with modern Ember patterns that will set us up for success in the future. And finally, after this talk, we’ll have a resource to which we can point future engineers when they scratch their heads about how to avoid these modifiers.

Avatar for Aaron Chambers

Aaron Chambers

September 23, 2022
Tweet

More Decks by Aaron Chambers

Other Decks in Programming

Transcript

  1. Goal for this talk… What are render-modifiers and why do

    they exist? What are the alternatives? Why are render-modifiers discouraged?
  2. Goal for this talk… What are render-modifiers and why do

    they exist? Why are render-modifiers discouraged? What are the alternatives?
  3. Goal for this talk… What are render-modifiers and why do

    they exist? Why are render-modifiers discouraged? What are the alternatives?
  4. Goal for this talk… What are render-modifiers and why do

    they exist? Why are render-modifiers discouraged? What are the alternatives?
  5. Glimmer Component (with render-modifiers) Initializing a library Unused DOM element

    passed to will-destroy function Setup/Teardown of the calendar is not logically grouped together Unnecessary DOM element
  6. Initializing a library Encapsulation of behaviour in logical package Glimmer

    Component (with custom-modifiers) Sensible separation of concerns No unnecessary DOM elements
  7. Glimmer Component (with render-modifiers) Adding/Removing Event Listeners Unused DOM element

    passed to functions Setup/Teardown not logically grouped together Unnecessary DOM element
  8. Adding/Removing Event Listeners Glimmer Component (with component hooks) Encapsulation of

    behaviour in logical package Registering destructor instead of relying on lifecycles No unnecessary DOM elements
  9. Adding/Removing Event Listeners When adding/removing event listeners, use component’s constructor

    and register a destructor function. (or use a custom modifier) Takeaway
  10. Fetching/ Re-Fetching Data Glimmer Component (with render-modifiers) Unused DOM element

    passed to functions Fetch/Re-fetch not logically grouped together Unnecessary DOM element Eagerly fetched
  11. Fetching/ Re-Fetching Data Glimmer Component (with resources) Reactivity Fetching behaviour

    bundled into shareable resource No unnecessary DOM elements Lazy loaded/executed
  12. Fetching/ Re-Fetching Data https://github.com/NullVoxPopuli/ember-resources Prefer Resources, which are reactive and

    lazily executed and encapsulate behaviour that is easily shareable. Takeaway
  13. Instead of render-modifiers consider… Custom modifiers Component constructor and registerDestructor

    Resources DOM interaction Adding/removing event listeners Fetching data
  14. One final thought Think not in terms of component lifecycles

    Think in terms of encapsulated behaviour