My sweet shadow — ComputedProperty Getters & Setters with Ember.js

Heinz Romirer
May 22, 2017 · 3 min read

The problem

What the user expects: the number in “correct” format
What the application expects: the number in correct format

The question

The solution: Ember.computed to the rescue!

// x-number-wrapper.js
export default Ember.Component.extend({
numberConverter: Ember.inject.service(),
value: 0, userValue: Ember.computed(‘value’, {
get () {
return this.get("numberConverter")
.format(this.get(‘value’), ‘,’);
},
set (key, value) {
this.set(‘value’,
this.get("numberConverter").parse(value, ‘,’)
);
return value;
}
})
});
// x-number-wrapper.hbs
<input type="text" value={{userValue}}/>
// data-sheet.hbs
<label for"width">St&auml;rke gegeben <span class="text-small">(in m)</span>:</label>
{{x-number-wrapper value=width}}

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade