Skip to content

Instantly share code, notes, and snippets.

@shearichard
Last active November 29, 2020 22:33
Show Gist options
  • Save shearichard/f782c6b176f42ffb0e09f2c003e094a0 to your computer and use it in GitHub Desktop.
Save shearichard/f782c6b176f42ffb0e09f2c003e094a0 to your computer and use it in GitHub Desktop.
Ember.js : Passing actions up through nested components

Ember.js - passing actions through nested components

Overview

This is a set of files which illustrate how actions are passed through a series of nested components. I'm putting this in a gist because this is one of those things which I often scratch my head over when I have to do it from scratch.

The example makes use of a set of components, each nested inside the other. The innermost component, test-component-inner exposes a button with the label 'PRESS ME', clicking the button has the result that a log message is written to the console by the outermost component, test-component-outer.

Application.hbs
+-----------------------------------------------------------------------------------------------+
|                                                                                               |
|                                                                                               |
|  test-component-outer.hbs                                                                     |
|  +-----------------------------------------------------------------------------------------+  |
|  |                                                                                         |  |
|  |                                                                                         |  |
|  |  test-component-middle.hbs                                                              |  |
|  |  +----------------------------------------------------------------------------------+   |  |
|  |  |                                                                                  |   |  |
|  |  |                                                                                  |   |  |
|  |  |  test-component-inner.hbs                                                        |   |  |
|  |  |  +---------------------------------------------------------------------------+   |   |  |
|  |  |  |  +----------+                                                             |   |   |  |
|  |  |  |  |          |                                                             |   |   |  |
|  |  |  |  | PRESS ME |                                                             |   |   |  |
|  |  |  |  |          |                                                             |   |   |  |
|  |  |  |  +----------+                                                             |   |   |  |
|  |  |  +---------------------------------------------------------------------------+   |   |  |
|  |  |                                                                                  |   |  |
|  |  +----------------------------------------------------------------------------------+   |  |
|  |                                                                                         |  |
|  +-----------------------------------------------------------------------------------------+  |
|                                                                                               |
+-----------------------------------------------------------------------------------------------+

Version

This is valid as of Ember 3.4

Reference

The corresponding documentation for these ideas is here : https://guides.emberjs.com/v3.4.0/components/triggering-changes-with-actions/#toc_calling-actions-up-multiple-component-layers

<div class="container-fluid">
<h1>Component Testbed</h1>
{{test-component-outer}}
</div>
<p>This is test-component-inner</p>
<p><button {{action testAction}}>Press Me</button></p>
{{yield}}
import Component from '@ember/component';
export default Component.extend({
});
<p>This is test-component-middle</p>
{{test-component-inner
testAction=(action testAction)
}}
{{yield}}
import Component from '@ember/component';
export default Component.extend({
});
<p>This is test-component-outer</p>
{{test-component-middle
testAction=(action "testAction")
}}
{{yield}}
import Component from '@ember/component';
export default Component.extend({
actions:{
testAction() {
console.log("testAction firing in test-component-outer");
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment