Skip to content

Instantly share code, notes, and snippets.

@trabus
Last active April 8, 2016 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trabus/4a05098ac1df27b6ffa0252706dc0182 to your computer and use it in GitHub Desktop.
Save trabus/4a05098ac1df27b6ffa0252706dc0182 to your computer and use it in GitHub Desktop.
Funky Computed Property
import Ember from 'ember';
const {Component, computed} = Ember;
export default Component.extend({
mything: null,
// thing value is also passed into the component
// thing watches no keys, and is not consumed
thing: computed({
// the getter is ignored entirely
set(k, v) {
// do any amount of work here
// could replace observer
console.log('do a mything');
//affect other property on change
this.set('mything', v);
return v;
}
}),
actions: {
setmything(val) {
this.set('mything', val);
}
}
});
import Ember from 'ember';
const {Controller, computed} = Ember;
export default Controller.extend({
appName: 'Ember Twiddle',
thing: computed({
get() {
// default
console.log('thing get');
return 'nothing';
},
set(k, v) {
// do something when the value changes
console.log('do a thing');
return v;
}
}),
actions: {
reset() {
this.set('thing', 'nothing');
},
setThing(val) {
console.log('setting thing');
this.set('thing', val);
}
}
});
<h1>Funky Computed Usage?</h1>
<br>
{{outlet}}
<div>
Application:<br>The thing is "{{thing}}"
</div>
<br>
<input value={{thing}} onchange={{action 'setThing' value="target.value"}}/>
<button onclick={{action 'setThing' thing}}>set</button>
<br>
<button onclick={{action 'reset'}}>reset</button>
<br>
<br>
<div>
Component:
{{foo-bar thing=thing}}
</div>
<div>
<p>The application controller has a computed property 'thing'. The 'thing' cp is passed into the foo-bar component.</p>
<p>The foo-bar component also has 'thing' as a computed property, setting the value of 'mything' in the setter of the computed property.</p>
<p>By using the setter of the 'thing' cp in the foo-bar component, we can call functions react to changes, similar to as if we'd set up an observer.</p>
<p>The 'thing' cp getter is not required, as it is completely ignored and never called/used.</p>
</div>
{{#if mything}}
<div>My thing is "{{mything}}"</div>
{{else}}
<div>Sorry, no thing</div>
{{/if}}
<div>
My thing input:
<input value={{mything}} onchange={{action 'setmything' value="target.value"}}/>
</div>
{
"version": "0.7.2",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment