Skip to content

Instantly share code, notes, and snippets.

@tomoguisuru
Last active August 27, 2018 13:45
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 tomoguisuru/3d506b8a31e0c27acebca0cd1dc27b3d to your computer and use it in GitHub Desktop.
Save tomoguisuru/3d506b8a31e0c27acebca0cd1dc27b3d to your computer and use it in GitHub Desktop.
Computed Prop (get/set)
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'get/set Demo',
model: null,
valueOne: 'Hello',
valueTwo: Ember.computed('valueOne', function() {
return this.get('valueOne') + ' from two';
}),
valueThree: Ember.computed('valueOne', {
get() {
return this.get('valueOne');
},
set(p, v) {
return v;
}
})
});
<h1>Welcome to {{appName}}</h1>
<p>
Value 2 is originally a computed value of Value 1.
</p>
<p>
However, if you manually <strong>set</strong> Value 2, it will no longer act as a computed value of Value 1.
</p>
<p>
If you need to manually set a <strong>comptued</strong> value, you a <strong>getter</strong> and <strong>setter</strong> as seen with Value 3.
</p>
<br>
<br>
Value 1:
{{input value=valueOne}}
<br>
<br>
Value 2:
{{input value=valueTwo}}
<br>
<br>
Value 3:
{{input value=valueThree}}
{
"version": "0.14.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": "2.18.2",
"ember-template-compiler": "2.18.2",
"ember-testing": "2.18.2"
},
"addons": {
"ember-data": "2.18.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment