<?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 Pragatheeswaran Sornarajan on Medium]]></title>
        <description><![CDATA[Stories by Pragatheeswaran Sornarajan on Medium]]></description>
        <link>https://medium.com/@pragatheeswaransornarajan?source=rss-5c9e973d8033------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jBM4ueejzMQmHBikoVaTvA.jpeg</url>
            <title>Stories by Pragatheeswaran Sornarajan on Medium</title>
            <link>https://medium.com/@pragatheeswaransornarajan?source=rss-5c9e973d8033------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 08:17:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pragatheeswaransornarajan/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[Input Modifiers using ember-modifier]]></title>
            <link>https://medium.com/clark-engineering/input-modifiers-using-ember-modifier-bf09e98e7362?source=rss-5c9e973d8033------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf09e98e7362</guid>
            <category><![CDATA[emberjs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[clark]]></category>
            <category><![CDATA[ember]]></category>
            <category><![CDATA[element-modifiers]]></category>
            <dc:creator><![CDATA[Pragatheeswaran Sornarajan]]></dc:creator>
            <pubDate>Wed, 27 Apr 2022 11:17:43 GMT</pubDate>
            <atom:updated>2022-04-27T11:17:43.922Z</atom:updated>
            <content:encoded><![CDATA[<h3>Input modifiers using ember-modifier</h3><p>Have you ever wondered what is this magic word modifier used in Ember applications? Imagine a customer needs to enter their salary data and it would be a nice user experience if you can format the value with thousands separators as and when they type in. Something like this,</p><figure><img alt="An input field formatting the value as a currency" src="https://cdn-images-1.medium.com/max/1024/1*eWOLNPjFrznRGQtk_kGjjg.gif" /><figcaption>An input field formatting the value as a currency</figcaption></figure><p>Usually, we would create a component to wrap input[type=&quot;text&quot;]. When the input event is triggered, we would format the value. That’s when our colleague Isaac Lee suggested that we could move the formatting to a modifier. By doing so, we can attach the modifier to any input element irrespective of the style of the element while keeping the component code simple.</p><figure><img alt="Representation of ideas — Hand holding a light bulb" src="https://cdn-images-1.medium.com/max/1024/1*x7Y6F_1nk794zOOHnisyKg.png" /><figcaption>Share ideas with other people &amp; make something new</figcaption></figure><p>I liked the idea, but I have never written a modifier before and this was a chance to learn something new while you work. It was not as complex as I thought it would be. Let me share how intuitive modifiers are and how to write one.</p><h3>What is a modifier?</h3><p>A modifier (in Ember JS) is a small piece of functionality that you can attach to any Document Object Model (DOM) element and interact with the element to perform various actions.</p><p>When the Octane edition was released, Ember added a built-in modifier called the on modifier. It calls an action when the user performs a specific event on the DOM element.</p><p>For example,</p><pre>&lt;button {{on &quot;click&quot; this.handleClick}} ... /&gt;</pre><p>Here, the role of the on modifier is to listen to the click event emitted by the button element and call the function which is passed as the second argument. Prior to Octane, we would use {{action}} helper.</p><p>on is a built-in modifier provided by Ember. Now, let’s see how we can create one of our own. For this, the Ember community has a great addon called <a href="https://github.com/ember-modifier/ember-modifier">ember-modifier</a>.</p><h3>Types of modifiers</h3><p>Before seeing how to create a modifier, let’s see the types of modifiers.</p><h4>Class-based modifiers</h4><p>This type of modifier exports a class that extends the Modifier class from the ember-modifier addon. It has a constructor and a modify hook that receives the following arguments.</p><ul><li>Element to which the modifier is attached to</li><li>An array of positional arguments</li><li>An object of named arguments</li></ul><p>It’s great to create this type of modifier if you want to inject services or store some value within the modifier and reuse it.</p><p><em>Note</em>: <em>Older versions of the ember-modifier addon exposed life-cycle hooks like </em><em>didInstall, </em><em>didReceiveArguments, </em><em>didUpdateArguments etc. which are deprecated now.</em></p><h4>Function-based modifiers</h4><p>This type of modifier exports a single default function that receives the same arguments as mentioned above. This is great for writing simple modifiers to perform basic operations. We have a good <a href="https://guides.emberjs.com/release/components/template-lifecycle-dom-and-modifiers/#toc_abstracting-the-logic-into-a-custom-modifier">example</a> of this in our Ember Guides.</p><blockquote>I prefer using named arguments when the number of arguments is more than two or when the arguments are passed from the template. It makes the code easier to understand and maintain.</blockquote><h3>Writing a modifier</h3><p>Let’s jump right into the task of writing the modifier for formatting the input field’s value as a currency. The code to use the modifier would be something like this.</p><pre>&lt;label for=&quot;1&quot;&gt;<br>  What is your annual gross salary (in EUR)?<br>&lt;/label&gt;</pre><pre>&lt;input<br>  id=&quot;1&quot;<br>  name=&quot;salary&quot;<br>  placeholder=&quot;Eg: 45000&quot;<br>  type=&quot;text&quot;<br>  {{format-currency locale=&quot;de-DE&quot; name=&quot;EUR&quot;}}<br>/&gt;</pre><h4>Prerequisites</h4><p>Let’s create a new Ember application (you can also create an Ember addon if you prefer) and install the addon ember-modifier</p><pre># Creating a new Ember application<br>ember new modifiers-demo<br><br># Go to the directory<br>cd modifiers-demo<br><br># Installing the ember-modifier addon<br>ember install ember-modifier</pre><p>Now, let’s create a <em>class-based</em> modifier.</p><pre># Generating a class-based modifier using the blueprint<br>ember g modifier format-currency --class</pre><p>If you open the file <em>app/modifiers/format-currency.js,</em> you can see that the blueprint has already exported a default class for you.</p><pre>import Modifier from &#39;ember-modifier&#39;;</pre><pre>export default class FormatCurrencyModifier extends Modifier {}</pre><h4>Step 1 of 4: Adding an event listener</h4><p>For our requirement, all we want to do is listen to the input event emitted from the input element and format the value and set it back in event.target.value.</p><p>To do this, let’s set up an event listener in the modify hook of the class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c15c4e37ba01f59c6442c81a6dfc675/href">https://medium.com/media/0c15c4e37ba01f59c6442c81a6dfc675/href</a></iframe><p>Now, the method handleInput will be called whenever the input event is triggered on the element. The method passed to registerDestructor will be called when the element is about to be destroyed from the DOM so that you can perform some cleanup operations.</p><h4>Step 2 of 4: Adding the logic to format the value</h4><p>Let’s add the formatting logic to the modifier. We will format the value using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat">Intl.NumberFormat</a> object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/193067b47ca7be8b08c9f937c929e7e8/href">https://medium.com/media/193067b47ca7be8b08c9f937c929e7e8/href</a></iframe><h4>Step 3 of 4: Remove non-numeric inputs and check for invalid inputs</h4><p>Now, the modifier should actually start formatting the input as you type in a value. Since we are using a text field, the user can enter any value in this text input. So, let’s remove the non-numeric characters and check for invalid inputs before passing it to Intl.NumberFormat.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc1604d3cf9ff5252eb5f06f4a63586f/href">https://medium.com/media/fc1604d3cf9ff5252eb5f06f4a63586f/href</a></iframe><h4>Step 4 of 4: Pass currency locale and currency name as arguments</h4><p>The modifier is almost ready. But we have hard coded the currency name and locale in the formatAsCurrency function. Let’s get it as arguments for the modifier so that you can use the modifier to format the currency for any locale.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af59e5ce8cbf37bbdc68a579825fdb4c/href">https://medium.com/media/af59e5ce8cbf37bbdc68a579825fdb4c/href</a></iframe><p>That’s it. The modifier is ready to convert any text field to a currency field. Now, you can attach it to an input[type=&quot;text&quot;] element to modify the value to a formatted currency as the user types the value.</p><blockquote>You can customise the <em>sanitizeValue</em> method to support decimal separators and thousands separators also (<em>,</em> and <em>.</em>) so that the users can enter floating point currency values.</blockquote><h3>About CLARK</h3><p>At <a href="http://www.clark.de/"><strong>CLARK</strong></a><strong>,</strong> we believe that <strong>people deserve peace of mind</strong>. With that vision in mind, we set out on our mission. <strong>CLARK</strong> makes it <strong>easy to get properly insured</strong>. We created an app that makes it<strong> easier</strong>, <strong>simpler</strong>, and <strong>faster</strong> for consumers to manage their insurances — all in one central place where changes can be implemented with just a few clicks.</p><p>We use<strong> Ember JS</strong> for the frontend, <strong>Ruby on Rails</strong> for the backend, <strong>AWS</strong> to host our application and <strong>Capacitor JS </strong>for our native Android &amp; IOS applications. Are you interested in any/all of the above tech stacks? Come, Join us in our journey of becoming the largest Insurance broker in Europe. Feel free to apply to or contact me if any of the <a href="https://www.clark.de/de/jobs">open positions</a> interests you.</p><figure><a href="https://www.clark.de/de/jobs"><img alt="An illustration depicting that CLARK is hiring for multiple positions" src="https://cdn-images-1.medium.com/max/1024/1*p5qBV9Eec8zCsqr-4xrAGw.png" /></a><figcaption>CLARK is hiring for multiple positions</figcaption></figure><h3>References:</h3><ul><li><a href="https://github.com/ember-modifier/ember-modifier">Ember Modifier Addon</a></li><li><a href="https://github.com/emberjs/rfcs/blob/master/text/0373-Element-Modifier-Managers.md">Element Modifier Manager RFC</a></li><li><a href="https://guides.emberjs.com/">Ember JS Guides</a></li><li><a href="https://2022.emberconf.com/talks/element-modifiers-under-the-hood">Element Modifiers: Under the Hood</a> — EmberConf 2022 talk by Jai Bhagat</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf09e98e7362" width="1" height="1" alt=""><hr><p><a href="https://medium.com/clark-engineering/input-modifiers-using-ember-modifier-bf09e98e7362">Input Modifiers using ember-modifier</a> was originally published in <a href="https://medium.com/clark-engineering">Clark Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>