<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Heinz Romirer on Medium]]></title>
        <description><![CDATA[Stories by Heinz Romirer on Medium]]></description>
        <link>https://medium.com/@herom?source=rss-2cfad216b7e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cRhGTLT5GuDn-sO4.</url>
            <title>Stories by Heinz Romirer on Medium</title>
            <link>https://medium.com/@herom?source=rss-2cfad216b7e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 03:37:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@herom/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My sweet shadow — ComputedProperty Getters & Setters with Ember.js]]></title>
            <link>https://medium.com/@herom/my-sweet-shadow-computedproperty-getters-setters-with-ember-js-b89d925e06dd?source=rss-2cfad216b7e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/b89d925e06dd</guid>
            <category><![CDATA[internationalization]]></category>
            <category><![CDATA[ember]]></category>
            <dc:creator><![CDATA[Heinz Romirer]]></dc:creator>
            <pubDate>Mon, 22 May 2017 19:45:10 GMT</pubDate>
            <atom:updated>2017-05-22T19:45:10.496Z</atom:updated>
            <content:encoded><![CDATA[<p>As I’m currently working on a project which is heavily based on numeric (float) data in JavaScript (using the amazing <a href="https://emberjs.com/">Ember.js</a> framework)I thought I’ll share a cool <strong>toolbelt </strong>for dealing with “internationalized numbers” (in terms of dealing with different number formattings and decimal separators).</p><h4>The problem</h4><p>To explain the “problem” I had in short terms: <br>The application itself is fed with numeric user-data all over the place, and the data is expected to be given in SI-units (meter, kilogramm, … you name it). The usual dimensions of the data is nearly never an <em>integer (1, 2, ..) </em>but rather a <em>float (1.2, 4.821, …) </em>value.</p><p>So, the application needs to work with correct JavaScript <strong>Number</strong> values, but it should represent the formatted output — with the power of<strong> two-way-bindings</strong> and “just as you type” updates of other computed values on the same screen, of course.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*bq_EFJ2tsqmt1GkQyvXw3w.jpeg" /><figcaption>What the user expects: the number in “correct” format</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*Sk0pfNA4RWAbb0_n7EHA4g.jpeg" /><figcaption>What the application expects: the number in correct format</figcaption></figure><h4>The question</h4><p>How would you deal with the “as you type” number-formatting and update of several dependent computations on the same page with the power of <a href="https://guides.emberjs.com/v2.3.0/object-model/computed-properties/#toc_setting-computed-properties">Ember.js</a> two-way-bindings?</p><p>First, we’d need to have a little Util or Servicewhich will be responsible for transferring the number from the <em>user-readable</em> to the <em>application-understandable</em> format. Next, we’ll need to update the number of the input-field as the user types. We could do this with a Helper and pass the value through this helper, but this would leave us with a cluttered mess and NaN all over the place (to make it short).</p><p>Now, let’s keep this Ember.Service class (we’ll call it number-converter) and see what we can do with it.</p><h4>The solution: Ember.computed to the rescue!</h4><p><a href="https://guides.emberjs.com/v2.3.0/object-model/computed-properties/#toc_setting-computed-properties">It is actually also mentioned within the Ember.js docs</a>, but I couldn’t think of any good or “in practice” use-case for the ComputedProperty getter/setter solution until I had to deal with this number-conversion problem myself. And even then, I had to re-read the docs in order to get my eye caught on this really elegant and — yes, I call it this way — awesome solution!</p><p>With Ember.computed getter/setter, the solution is actually pretty easy:</p><pre>// x-number-wrapper.js<br><strong>export default </strong>Ember.Component.extend({<br>  numberConverter: Ember.inject.service(),  </pre><pre>  value: 0,</pre><pre>  userValue: Ember.computed(‘value’, {<br>    get () {<br>      <strong>r</strong>eturn this.get(&quot;numberConverter&quot;)<br>               .format(this.get(‘value’), ‘,’);<br>    },</pre><pre>    set (key, value) {<br>      <strong>this</strong>.set(‘value’, <br>                this.get(&quot;numberConverter&quot;).parse(value, ‘,’)<br>      );</pre><pre><strong>      return </strong>value;<br>    }<br> })<br>});</pre><pre>// x-number-wrapper.hbs<br>&lt;input type=&quot;text&quot; value={{userValue}}/&gt;</pre><p>Now, we can just use it in our application:</p><pre>// data-sheet.hbs<br>&lt;label for&quot;width&quot;&gt;St&amp;auml;rke gegeben &lt;span class=&quot;text-small&quot;&gt;(in m)&lt;/span&gt;:&lt;/label&gt;<br>{{x-number-wrapper value=width}}</pre><p>And that’s it! Neat, isn’t it? :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b89d925e06dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>