<?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 Frederico Pavão on Medium]]></title>
        <description><![CDATA[Stories by Frederico Pavão on Medium]]></description>
        <link>https://medium.com/@frederico.pavao_39101?source=rss-cbd1a9beaafb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*01c-KuA5tGY5F4nQ</url>
            <title>Stories by Frederico Pavão on Medium</title>
            <link>https://medium.com/@frederico.pavao_39101?source=rss-cbd1a9beaafb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 15:54:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@frederico.pavao_39101/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[Outsystems and MutationObserver — Use cases]]></title>
            <link>https://medium.com/valanticlcs/outsystems-and-mutationobserver-use-cases-78c56a5b03d5?source=rss-cbd1a9beaafb------2</link>
            <guid isPermaLink="false">https://medium.com/p/78c56a5b03d5</guid>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[outsystems]]></category>
            <category><![CDATA[mutationobserver]]></category>
            <dc:creator><![CDATA[Frederico Pavão]]></dc:creator>
            <pubDate>Fri, 28 Feb 2025 11:05:57 GMT</pubDate>
            <atom:updated>2025-02-28T11:12:28.260Z</atom:updated>
            <content:encoded><![CDATA[<h3>Outsystems and MutationObserver — Use cases</h3><p>Introduced in the W3C DOM4 specification, a <em>MutationObserver </em>is a relatively modern JavaScript feature that allows developers to watch for changes in the <em>DOM </em>(Document Object Model) of a webpage. It enables reactions to changes such as the addition or removal of elements, attribute modifications, or text content updates. <em>MutationObserver </em>has been continuously maintained and enhanced since its first version in 2012.</p><p>When programming in vanilla JavaScript or even within a frontend framework, it provides powerful features for a variety of use cases.</p><p>Some practical applications include:</p><ul><li>Dynamic Content Updates: automatically updating parts of the UI when content is added or changed;</li><li>Form Validation: monitoring form fields for changes to validate inputs dynamically;</li><li>Lazy Loading: loading additional content or resources when new elements are added to the DOM;</li><li>Tracking Changes: keeping track of changes for logging or debugging purposes;</li><li>Enhancing User Experience: providing real-time feedback or interactions based on DOM changes, like updating a shopping cart when items are added.</li></ul><h3>A practical example, with code</h3><p>To initialize a <em>MutationObserver </em>in JavaScript, we can follow these steps:</p><ol><li>Create a <em>MutationObserver </em>instance by passing a callback function that will be executed when mutations are observed;</li><li>Specify the target node and configuration options;</li><li>Start observing the target node.</li></ol><p>Here is a basic example:</p><pre>// Step 1: Create a MutationObserver instance<br>const observer = new MutationObserver((mutationsList, observer) =&gt; {<br>    for(let mutation of mutationsList) {<br>        if (mutation.type === &#39;childList&#39;) {<br>            console.log(&#39;A child node has been added or removed.&#39;);<br>        } else if (mutation.type === &#39;attributes&#39;) {<br>            console.log(&#39;An attribute was modified.&#39;);<br>        }<br>    }<br>});<br><br>// Step 2: Specify the target node and configuration options<br>const targetNode = document.getElementById(&#39;someElement&#39;);<br>const config = { attributes: true, childList: true, subtree: true };<br><br>// Step 3: Start observing the target node<br>observer.observe(targetNode, config);<br><br>// To stop observing<br>observer.disconnect();</pre><h3>Integrating with Outsystems — Where should I initialize my <em>MutationObserver</em>?</h3><p>As an OutSystems frontend developer, I’ve had a lot of success using this feature in Service Studio.</p><p>In many projects, we deal with concurrent requests that modify the DOM. It’s helpful to know when elements are inserted, updated, or removed so we can trigger client-side logic when these events occur.</p><p>When initializing a MutationObserver in a screen or web block, we have a couple of options:</p><ul><li>Inside the <em>OnInitialize </em>lifecycle hook or inside a required script — This approach is better when we want to watch changes on the <em>document </em>object, which represents the entire page;</li><li>Inside the <em>OnReady </em>lifecycle hook — This is useful when we want to watch for changes in an element that is inside our widget tree. The <em>OnReady </em>ensures that that element will be rendered and inserted in the DOM when the <em>MutationObserver </em>starts running.</li></ul><p>This concept is easier to understand through real-world use cases. So, without further ado:</p><h4>1) Calling a client action when an element is added to the DOM</h4><p>Let’s start with a very simple example of calling a client action when a DOM element has been added to our document. This can come in handy in situations where, for example, certain business rules cause an element inside an if node to become visible, and we might want to execute some client-side code in response to that.</p><p>Here’s how it would work:</p><ul><li>Assign an ID to the element that has conditional visibility;</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/156/1*hnTaYlifyEGOVRejOPuRDg.png" /><figcaption>Element is not initially in the DOM, but will be conditionally attached.</figcaption></figure><ul><li>Declare a mutation observer(here I’m using the <em>OnInitialize </em>hook of the screen, since we are watching for changes in the <em>document </em>object) and inject the element ID as an input parameter in the JavaScript node;</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*6t56lSWk1gczH8_St4b5ZQ.png" /><figcaption>Mutation observer will trigger when the element is inserted in the DOM.</figcaption></figure><ul><li>On the <em>MutationObserver </em>declaration, try to get the element from the DOM and if it exists, programmatically call the client action. Don’t forget to disconnect the observer so it does not keep running after the element has been attached to the DOM.</li></ul><h4>2) Executing JavaScript code after external data is loaded</h4><p>Throughout my career as an OutSystems frontend developer, I’ve often needed to execute JavaScript code on tables or other list-type widgets that depend on external data to be fully rendered</p><p>To achieve this, I’ve tried running said code in the <em>OnReady </em>lifecycle hook, but got little success doing so because data may not have been fully rendered at this point. <em>OnRender </em>can also be an option, but since it triggers multiple times it can be heavy on performance and we would need a control variable to know when data is loaded, to then execute our code.</p><p>This can all be easily achieved with a <em>MutationObserver </em>by following some steps. In this example we will be using a table widget as our UI element.</p><ul><li>Add a class to the table when data is loaded by using the <em>IsDataFetched </em>data source boolean property;</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EImgG9UpmF_WvJj9Ynm6aQ.png" /><figcaption>Adding a conditional class to the table widget using the <em>IsDataFetched</em> property.</figcaption></figure><ul><li>In the <em>MutationObserver</em>, check if the table contains the class. If it does, it means the data has been fully loaded and you can now execute your JavaScript code. In this case, since we are getting the table widget node by using <em>getElementById</em>, I would suggest running this code inside the <em>OnReady </em>lifecycle hook so we can assure that the table widget is already in the DOM.<br>We also use an attribute filter so the observer only watches for changes in the <em>class </em>attribute.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*srymlBEmL3yRUFeOTXbVDQ.png" /><figcaption>In this case we are observing the table widget instead of the entire document, for performance purposes.</figcaption></figure><h4>3) Watch for device type change</h4><p>OutSystems has a very useful feature that allows us to know what type of device we are currently using. A class with the device name is attached to the <em>body </em>element and responsively changes along with screen size. If you open your browser inspector, it’s easy to spot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*i1Tq2Mo76r--jNIDwjt1bA.png" /></figure><p>This means we are currently looking at a desktop-sized screen, with a horizontal display(landscape).</p><p>In some niche cases, such as a mobile device screen orientation change or dragging a browser window from one screen to another with drastically different sizes and aspect ratios, we may need to run JavaScript to recalculate the dimensions of a DOM element. This ensures it remains as responsive as possible and adapts to abrupt screen size changes.</p><p>This is made easy with a simple <em>MutationObserver</em>, where we watch for class changes in the <em>body </em>element and adapt accordingly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*2zlf07O7j5-k_zsMVdjEng.png" /></figure><p>We can filter screen specificity even more by searching for the classes <em>portrait </em>and <em>landscape </em>alongside the name of the device. This approach provides greater precision when needed. Keep in mind that device names and orientations are determined by OutSystems’ device detection and can be modified by the developer, but we will not cover that topic in this article.</p><h3>Conclusion</h3><p><em>MutationObserver </em>is a powerful feature in both low-code and high-code programming. It can fill gaps in the framework or enhance existing functionality.</p><p>There are countless applications for it, and developers can gain significant advantages by mastering this powerful JavaScript feature.</p><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78c56a5b03d5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/valanticlcs/outsystems-and-mutationobserver-use-cases-78c56a5b03d5">Outsystems and MutationObserver — Use cases</a> was originally published in <a href="https://medium.com/valanticlcs">valanticlcs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>