Skip to content

Instantly share code, notes, and snippets.

@jenweber
Last active July 29, 2018 20:00
Show Gist options
  • Save jenweber/190c45aa9e6d4e696251bb7dd45849ce to your computer and use it in GitHub Desktop.
Save jenweber/190c45aa9e6d4e696251bb7dd45849ce to your computer and use it in GitHub Desktop.
Passing actions from services, take 2
import Ember from 'ember';
export default Ember.Component.extend({
});
import Component from '@ember/component';
import { inject as service } from '@ember/service';
export default Component.extend({
alert: service(),
// component implementation
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
// alert.js
import Service from '@ember/service';
export default Ember.Service.extend({
actions: {
thisIsAServiceAction() {
alert('service action')
}
},
thisIsANormalMethod() {
alert('service method, not an action')
}
});
<h2>Things that work:</h2>
{{parent-component}}
<br>
<button {{action onConfirm}}>click: {{yield}}</button>
We can pass the service action method to a block form child component and it will work:
<!-- parent-component.hbs -->
{{#child-component onConfirm=(action "thisIsAServiceAction" target=alert)}}
yielded content
{{/child-component}}
<br>
We can pass the service action to a non block component and it will work:
{{child-component onConfirm=(action "thisIsAServiceAction" target=alert)}}
<br>
We can pass a _normal service method_ (non-action) as a variable:
{{child-component onConfirm=alert.thisIsANormalMethod}}
<br>
We can call a _normal service method_ (non-action) directly in the parent/home component:
<button {{action "thisIsANormalMethod" target=alert}}>parent component button</button>
<h2>Things that don't work</h2>
We cannot call a service action directly on the parent component. This does not work:
<button {{action "thisIsAServiceAction" target=alert}}>parent component button</button>
<br><br>
We cannot pass a regular method to any child component. These two will not work:
{{child-component onConfirm=(action "thisIsANormalMethod" target=alert)}}
{{#child-component onConfirm=(action "thisIsANormalMethod" target=alert)}}
yielded content
{{/child-component}}
{
"version": "0.14.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": "2.18.2",
"ember-template-compiler": "2.18.2",
"ember-testing": "2.18.2"
},
"addons": {
"ember-data": "2.18.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment