Skip to content

Instantly share code, notes, and snippets.

@chvonrohr
Last active September 27, 2018 14:43
Show Gist options
  • Save chvonrohr/55c2c93e5ed602bba215f2b076d52e9a to your computer and use it in GitHub Desktop.
Save chvonrohr/55c2c93e5ed602bba215f2b076d52e9a to your computer and use it in GitHub Desktop.
computed-property-qp
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
items: [
{key: 1, title: "ONE"},
{key: 2, title: "TWO"},
{key: 3, title: "THREE"}
],
queryParams: ['active'],
activeItem: Ember.computed('active', {
get(k) {
console.log(this.get('active'), 'getter');
return this.get('items').findBy('key', this.get('active'));
},
set(k, value) {
console.log(value, 'setter');
this.set('active', value.key);
return value;
}
}),
active:1,
actions: {
setItemObject(key) {
let obj = this.get('items').findBy('key', key);
this.set('activeItem', obj);
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
active: {{active}}
<br>title: {{activeItem.title}}
<br><br>
<a href="" {{action 'setItemObject' 1}}>1</a> |
<a href="" {{action 'setItemObject' 2}}>2</a> |
<a href="" {{action 'setItemObject' 3}}>3</a>
{
"version": "0.15.0",
"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.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment