Skip to content

Instantly share code, notes, and snippets.

@WenInCode
Created August 15, 2019 21:05
Show Gist options
  • Save WenInCode/27ccf6d8830f4179024d19d039fdf02f to your computer and use it in GitHub Desktop.
Save WenInCode/27ccf6d8830f4179024d19d039fdf02f to your computer and use it in GitHub Desktop.
computed prop key example
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
data: { cluster: { type: 'pg' }},
computedTopLevel: Ember.computed('data', function() {
return this.get('data.cluster.type') === 'pg' ? 'POSTGRES' : 'MYSQL';
}),
computedActualProp: Ember.computed('data.cluster.type', function() {
return this.get('data.cluster.type') === 'pg' ? 'POSTGRES' : 'MYSQL';
}),
actions: {
toggleType() {
if (this.get('data.cluster.type') === 'pg') {
this.set('data.cluster.type', 'mysql');
} else {
this.set('data.cluster.type', 'pg');
}
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
Computed top level: {{computedTopLevel}}<br>
Computed actual prop: {{computedActualProp}}<br>
<button {{action "toggleType"}}>Toggle cluster type</button>
<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