Skip to content

Instantly share code, notes, and snippets.

@pswai
Created March 13, 2017 04:01
Show Gist options
  • Save pswai/d7b42d2c6d480dda2972cef08563b4e5 to your computer and use it in GitHub Desktop.
Save pswai/d7b42d2c6d480dda2972cef08563b4e5 to your computer and use it in GitHub Desktop.
Function-style HOC
import Ember from 'ember';
import Foo from './foo-comp';
import decorate from './decorate-hoc';
export default decorate(Foo);
import Ember from 'ember';
import Foo from './foo-comp';
import log from './log-hoc';
export default log('error')(Foo);
import Ember from 'ember';
const decorateHoc = Component => {
return Component.extend({
actions: {
handleButtonClick(text) {
this._super(`Decorated: ${text}`);
}
}
});
};
export default decorateHoc;
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['foo'],
// This is important. Without this Ember won't pick up the right template
layoutName: 'components/foo-comp',
text: 'foo',
onSomethingClick: Ember.K,
actions: {
handleButtonClick(text) {
this.get('onSomethingClick')(`Clicked button in ${text}`);
},
handleTextClick(text) {
this.get('onSomethingClick')(`Clicked text in ${text}`);
}
}
});
import Ember from 'ember';
const logHoc = logLevel => Component => Component.extend({
actions: {
handleButtonClick(text) {
console[logLevel](`Passed in ${text}`);
this._super(...arguments);
}
}
});
export default logHoc;
import Ember from 'ember';
export default Ember.Controller.extend({
echoText: 'Click on the texts or the buttons to start',
actions: {
echo(text) {
this.set('echoText', text);
}
}
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.foo {
border: 1px solid lightgray;
padding: 10px;
margin-top: 10px;
}
<div>Echo text: {{echoText}}</div>
{{foo-comp text='foo' onSomethingClick=(action 'echo')}}
{{bar-comp text='bar' onSomethingClick=(action 'echo')}}
{{baz-comp text='baz' onSomethingClick=(action 'echo')}}
<div>
<div {{action 'handleTextClick' text}}>Text: {{text}}</div>
<div>
<button type="button" {{action 'handleButtonClick' text}}>{{text}}</button>
</div>
</div>
{
"version": "0.11.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.11.0",
"ember-data": "2.11.0",
"ember-template-compiler": "2.11.0",
"ember-testing": "2.11.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment