Skip to content

Instantly share code, notes, and snippets.

@baroquon
Last active August 21, 2019 23:14
Show Gist options
  • Save baroquon/1fa7679c7a4af16e13158247f7ac9fc8 to your computer and use it in GitHub Desktop.
Save baroquon/1fa7679c7a4af16e13158247f7ac9fc8 to your computer and use it in GitHub Desktop.
Context and Actions
import Ember from 'ember';
export default Ember.Component.extend({
label: "child-component"
});
import Ember from 'ember';
import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';
export default Ember.Component.extend({
myService: service(),
label: "grandchild-component"
});
import Ember from 'ember';
export default Ember.Component.extend({
label: 'parent-component',
someFunc() {
alert(`This is inside the parent-component but is in the ${this.label} context`);
},
actions: {
someAction() {
alert(`This is inside the parent-component but is in the ${this.label} context`);
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Lookin at some Actions'
});
import Ember from 'ember';
export default Ember.Service.extend({
label: 'my-service',
serviceFunc(){
alert(`This is inside my-service but is in the ${this.label} context`);
},
actions: {
serviceAction(){
alert(`This is inside my-service but is in the ${this.label} context`);
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<ParentComponent />
CHILD COMPONENT
{{yield}}
<GrandchildComponent
@someFunc={{someFunc}}
@someActionFunc={{action someFunc}}
@someAction={{action someAction}} />
{{yield}}
<br>
<h3>Component Actions</h3>
<button onclick={{action @someFunc}}>Some Func</button>
<button onclick={{action @someActionFunc}}>Some Action Func</button>
<button onclick={{action @someAction}}>Some Action</button>
<h3>Service Actions</h3>
<button onclick={{action myService.serviceFunc}}>Some Service Function</button>
<button onclick={{action 'serviceAction' target=this.myService}}>Some Service Action</button>
{{yield}}
PARENT COMPONENT
<ChildComponent
@someFunc={{someFunc}}
@someAction={{action 'someAction'}} />
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"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