> ## Documentation Index
> Fetch the complete documentation index at: https://domoinc-openapi-sync-dataflows.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Handling Data Updates

This is an example of a simple app that leverages the [`domo.onDataUpdated`](/portal/Apps/App-Framework/Tools/domo-js#domo-ondataupdated-callback) function to manage how the app responds to updates made to the underlying DataSet. Without this function, the app would be reloaded each time that underlying data was updated, which could lead to a bad user experience if the app refreshed in the middle of a user interaction. This is particularly painful with SPA apps where the user may have navigated to a sub route in the app and then get kicked back to the main route because the data refreshed.

### Getting Started

***

#### **Sample Data**

Copy the table below and paste it into a new Domo Webform, then note the DataSet ID.

<table>
  <tbody>
    <tr><td><strong>name</strong></td><td><strong>email</strong></td><td><strong>total</strong></td></tr>
    <tr><td>Alice Johnson</td><td>[alice.johnson@example.com](mailto:alice.johnson@example.com)</td><td>8240</td></tr>
    <tr><td>Bob Martinez</td><td>[bob.martinez@example.com](mailto:bob.martinez@example.com)</td><td>5130</td></tr>
    <tr><td>Carol White</td><td>[carol.white@example.com](mailto:carol.white@example.com)</td><td>9870</td></tr>
    <tr><td>David Kim</td><td>[david.kim@example.com](mailto:david.kim@example.com)</td><td>4560</td></tr>
    <tr><td>Eva Rossi</td><td>[eva.rossi@example.com](mailto:eva.rossi@example.com)</td><td>7810</td></tr>
    <tr><td>Frank Nguyen</td><td>[frank.nguyen@example.com](mailto:frank.nguyen@example.com)</td><td>3290</td></tr>
    <tr><td>Grace Chen</td><td>[grace.chen@example.com](mailto:grace.chen@example.com)</td><td>6450</td></tr>
    <tr><td>Henry Okafor</td><td>[henry.okafor@example.com](mailto:henry.okafor@example.com)</td><td>11200</td></tr>
  </tbody>
</table>

#### **domo init**

Run the `domo init` command.

* design name: live-data-demo
* select a starter: hello world
* would you like to connect to any datasets?: Yes
* dataset id: the Id of the DataSet you just created
* dataset alias: sales
* add another dataset? No

#### **index.html**

Replace the `index.html` content with the following:

```html theme={"dark"}
<html>
  <head>
    <link rel="stylesheet" href="app.css" />
  </head>
  <body>
    <h1>Handling Data</h1>

    <h2>Total Sales</h2>
    <div>$ <span id="total">0</span></div>

    <h4># Data Fetches</h4>
    <div id="updateCount">0</div>

    <!-- domo.js optional utils -->
    <script src="https://unpkg.com/ryuu.js"></script>
    <script src="app.js"></script>
  </body>
</html>
```

#### **Publish**

**Add a thumbnail**

Download the sample below and place it in your project root as `thumbnail.png`. The file must be exactly 300×300 pixels.

<a href="/images/thumbnail.png" download="thumbnail.png">Download thumbnail.png (300×300)</a>

**Publish**

```bash theme={"dark"}
domo publish
```

### Development

***

We'll be making a few changes to the `app.js` file. Replace the current contents with the following:

```js theme={"dark"}
let counter = 0;
let total = 0;

function getData(alias) {
  counter += 1;
  domo.get(`/data/v1/${alias}?sum=total`).then((data) => {
    total = data[0].total;
    updateUI();
  });
}

function updateUI() {
  document.getElementById('total').innerHTML = total;
  document.getElementById('updateCount').innerHTML = counter;
}

getData('sales');
```

In its current state, the `counter` variable would never increment. It would always show as `1` because the app would always do a full refresh each time the data updated.

Publish the design, create a new app card using that design, and then in a new tab update a row in the Webform DataSet — watch the app card to see it reload.

<img src="https://mintcdn.com/domoinc-openapi-sync-dataflows/dsMwyHmDUK-tWvgV/images/card-reloading.gif?s=aa3e07d264ec9628d179304498e06c14" alt="App card reloading after a DataSet update" width="354" height="374" data-path="images/card-reloading.gif" />

#### Adding `domo.onDataUpdated()`

See how the app did a full page reload after the DataSet was updated and the counter remained at `1`? Now we'll add a [`domo.onDataUpdated()`](/portal/Apps/App-Framework/Tools/domo-js#domo-ondataupdated-callback) event listener to take control of how our app responds to data updates. Add the following function inside your script.

```js theme={"dark"}
domo.onDataUpdated(getData);
```

<Warning>
  **Deprecated method name:** `domo.onDataUpdate()` (without the trailing `d`) was the v4 API and no longer exists in current versions of ryuu.js. Use `domo.onDataUpdated()`.
</Warning>

Publish again, then refresh the card page to pick up the new code. Now each time you update the DataSet your app should update the counter instead of reloading.

<img src="https://mintcdn.com/domoinc-openapi-sync-dataflows/LLmKzFBc-X6-fVQ_/images/handling-data.gif?s=c757ac74a590c681b79b317e59fdd356" alt="Handling data updates without a page reload" width="354" height="374" data-path="images/handling-data.gif" />
