Skip to content

Instantly share code, notes, and snippets.

@lukecoy
Last active August 8, 2019 21:53
Show Gist options
  • Save lukecoy/301155e3e0230ae22c8c65aa883ccecb to your computer and use it in GitHub Desktop.
Save lukecoy/301155e3e0230ae22c8c65aa883ccecb to your computer and use it in GitHub Desktop.
controller bind
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
actionThatShouldNotBeOverriden() {
console.log('yay');
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return { foo: 'bar' };
},
actionOne() {
this.set('controller.model.foo', 'Mutation 1!');
},
actionTwo() {
this.set('controller.model.foo', 'Mutation 2!');
},
setupController(controller) {
this._super(...arguments);
if (!controller.get('actions')) controller.set('actions', {});
controller.setProperties({
'actions.actionOne': this.get('actionOne').bind(this),
'actions.actionTwo': this.get('actionTwo').bind(this)
});
if (controller.get('actions.actionThatShouldNotBeOverriden')) {
this.set('controller.model.baz', 'The native controller action is still defined :)')
}
}
});
<h1>Welcome!</h1>
<button onclick={{action "actionOne"}}>do actionOne!</button>
<button onclick={{action "actionTwo"}}>do actionTwo!</button>
<button onclick={{action "actionThatShouldNotBeOverriden"}}>do actionThatShouldNotBeOverriden!</button>
<br>
<br>
{{model.foo}}
{{model.baz}}
<br>
<br>
{
"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