Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NullVoxPopuli/9cf5ca01a0758beaf6e9394f0bffecfa to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/9cf5ca01a0758beaf6e9394f0bffecfa to your computer and use it in GitHub Desktop.
Example of passing actions from controller to nested component
import Ember from 'ember';
export default Ember.Component.extend({
// this component only received args from the caller
// but it does track its own local info property
info: '', // initial form field value
});
<div class='border'>
<span>a-teeny-component -- local info: {{this.info}}</span>
{{input value=this.info placeholder='this info is two-way bound locally to the component'}}
<button onclick={{action @sendTheForm this.info}}>submit</button>
</div>
import Ember from 'ember';
import { computed } from '@ember/object';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
info: '',
aComplexInfo: {
nestedData: ''
},
dataDisplay: computed('aComplexInfo.nestedData', function() {
return JSON.stringify(this.aComplexInfo);
}),
actions: {
sendTheForm(info) {
console.log('sendTheForm received', info);
this.set('info', info);
},
updateNestedData(nestedData) {
console.log('updateNestedData received', nestedData);
this.set('aComplexInfo.nestedData', nestedData);
}
}
});
import Ember from 'ember';
export function stringify(params/*, hash*/) {
return JSON.stringify(params[0]);
}
export default Ember.Helper.helper(stringify);
import Ember from 'ember';
export default Ember.Component.extend({
// this component only receives args from the caller
});
<div class='border'>
<span>my-component - type something into the form below and click submit</span>
<ATeenyComponent @sendTheForm={{this.sendTheForm}} />
</div>
import Ember from 'ember';
export default Ember.Component.extend({
text: ''
});
<div class='border'>
<span>nested-data-form</span>
{{input value=this.text}}
<button onclick={{action @onSubmit this.text}}>submit</button>
<br><br>
Two way bound: <br>
{{input [email protected]}}
</div>
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.border {
border: 1px solid;
padding: 10px;
}
span {
display: block;
padding-bottom: 10px;
}
input[type='text'] {
width: 100%;
}
button {
margin-top: 5px;
}
<h1>Welcome to {{appName}}</h1>
Controller's Info: {{this.info}}
<br><br>
<MyComponent @sendTheForm={{action 'sendTheForm'}} />
<br><br>
Controller's Data: {{this.dataDisplay}}
<NestedDataForm
@data={{this.aComplexInfo}}
@onSubmit={{action 'updateNestedData'}}
/>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": true,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment