Skip to content

Instantly share code, notes, and snippets.

@courajs
Created April 12, 2017 13:25
Show Gist options
  • Save courajs/ef7a9a9b86de23d6abfad0de7d6b195f to your computer and use it in GitHub Desktop.
Save courajs/ef7a9a9b86de23d6abfad0de7d6b195f to your computer and use it in GitHub Desktop.
Trampolined Properties
import Ember from 'ember';
let t = 0;
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
theKey: 'one',
one: 1,
two: 2,
message: Ember.computed.alias('messageTrampoline.message'),
init() {
this._super(...arguments);
Ember.defineProperty(this, 'messageTrampoline', Ember.computed('theKey', function() {
let key = this.get('theKey');
return Ember.Object.extend({
message: Ember.computed('parent.'+key, function() {
let thing = this.get('parent.'+key);
return `Hi, ${thing}`;
})
}).create({parent: this});
}));
},
actions: {
bump(key) {
this.incrementProperty(key);
},
toggle() {
t++;
if (t % 2 === 0) {
this.set('theKey', 'one');
} else {
this.set('theKey', 'two');
}
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{message}}
<br>
<button {{action 'bump' 'one'}}>One</button>
<button {{action 'bump' 'two'}}>Two</button>
<button {{action 'toggle'}}>Toggle</button>
<br>
{
"version": "0.12.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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment