Hello everyone and welcome back to another edition of Readers’ Questions presented by the folks from The Ember Times ! This week I’m going to answer the following question by @erikrothoff (1):
It’s really true that DDAU is the Ember way, yet Ember itself does quite little to make this way of working easier/safer/more robust. Are there any plans or ideas to improve the experience with DDAU?
The so-called “Data Down, Actions Up (DDAU)” paradigm has been a popular practice in Ember applications since the early 1.x era (2). It is used to ensure that data flows unidirectionally between routes, controllers, parent and child components. Unidirectional data flow contributes to application state that is easier to manage and debug. If you want to learn more about what DDAU is and how it is applied, be sure to check out our answer to a previous Readers’ Question explaining the pattern (3). You might also find the EmberJS Guides about triggering application changes with action
s to be another useful resource to learn more about it (4).
But are there any plans to improve the developer experience for DDAU in Ember apps though? The clear answer is: Yes.
With Ember Octane, the upcoming first edition of Ember, the Glimmer Components API for building modern components will improve the developer experience for DDAU in your app significantly: in Glimmer components, arguments (properties which are passed into the component at its invocation site) are immutable (5). This means that any properties passed down to a component from a parent component or a controller cannot be directly modified on the child component, but instead need to be modified by the data owner - e.g. by triggering an action passed down from said data owner. There are exceptions to this behaviour though; to guarantee backwards compatibility, in Ember Octane it will still be possible to mutate arguments directly by 2-way-bound, classic components such as the built-in <Input />
when used inside of a Glimmer component (6).
The Named Arguments Syntax makes it easier to recognise immutable data, by clarifying where they are coming from (7). Immutable arguments (i.e. data passed down) are identified as @toto
in our component templates and as this.args.toto
in our component .js
file, whereas properties defined on the component itself are explicitly referred to as this.toto. This makes it easier for you to track down data in your components that need an update via DDAU.
Autotracking via the Tracked Properties feature on the other hand encourages us to write side-effect free getters and avoid state updates using observers, ultimately promoting unidirectional data flow in our Ember apps (8, 9).
Finally, the way in which we can define and pass around state-modifying methods in our application is being simplified with Ember Octane. The fn
helper, combined with the on
modifier will leverage the same functionalities that we are already used to when using the {{action}}
helper in classic Ember apps (10). In addition to that, fn
allows for more advanced use cases, including the application of several arguments to a method. Using fn
allows for the application of arguments in a clearer and more predictable fashion than the previous method of currying arguments with closure-based actions in classic Ember apps(11, 12).
And another reminder: You can already try out Ember Octane and the new DDAU experience in your apps today! Since Ember 3.13, the framework is feature-complete in regards to Octane. You can check out the Octane Edition Page (13) to review the emerging new Guides for the edition and enable its feature set in your app using the instructions from the official Ember 3.14 release announcement (14).
Last, but not least, a big thank you
goes to @void_malex, @ijlee2, @jenweber and @pzuraq who helped answering this Readers’ Question!
Sources
- 1: Comment to The Ember Times No. 112
- 2: EmberJS Guides - v1.10: “Sending Actions from Components to Your Application”
- 3: Readers’ Questions: “What is Meant by the Term ‘Data Down, Actions Up’?”
- 4: EmberJS Guides - v3.13: “Triggering Changes with Actions”
- 5: Immutable Arguments from RFC#416: Glimmer Components
- 6: On mut and 2-Way Data Binding by @pzuraq
- 7: RFC#276: Named Arguments
- 8: RFC#410: Tracked Properties
- 9: Ember Octane Update - Async Observers by @pzuraq
- 10: RFC#470: Fn Helper
- 11: Release Post: Ember 3.11 Released
- 12: Currying Arguments with Closure Actions from RFC#50: Improved Actions
- 13: Ember Octane Edition Page
- 14: Release Post: Ember 3.14 Released
This answer was published in Issue #125 of The Ember Times . Don’t forget to subscribe to the newsletter to get new Readers’ Questions and other development news right in your inbox. You can also submit your own questions! You will find a link at the bottom of the newsletter.
Do you feel up to answer community questions for another edition of The Ember Times yourself? Ping us anytime on the #support-ember-times
channel on the Ember Discord!
See you in the next issue!