ToolJet/docs/versioned_docs/version-2.25.0/how-to/import-external-lib-js.md
Abhinaba Adhikari fe086ac96c
Add new templates (#8907)
* update whitelist IP address and add warning in workspace constants to avoid usage in the client

* [docs] platform milestone 11 (#8847)

* [docs]reset password

* [docs]edit+archive flow

* [docs]updated docs in v2.29 and v2.30

* [docs]github ssouserinfo

* [docs] openid - id_token_encrypted

* Change docker image build's slack message

* [docs]archive/unarchive workspaces:superadmin (#8880)

* mention that workspace constants will not work with workspace constants (#8887)

* update release notes and project overview sidebar (#8884)

* add rgba example for timeline doc (#8892)

* update commands and solution for migration issues

* minor grammar fix

* [docs] revamped how-to guides (#8688)

* [docs]revamped howto: serverside pagination

* [docs]revamp howto: import ext. js libs

* [docs]revamp howto:run actions from js query

* [docs]revamp howto: intentionally fail js query

* updated run query at specified

* revamped how to: use to_py

* [docs]updated how to access users location

* updated howtos in v2.30

* Add new templates - batch 3

---------

Co-authored-by: Karan Rathod <karan.altcampus@gmail.com>
Co-authored-by: Adish M <44204658+adishM98@users.noreply.github.com>
Co-authored-by: Shubhendra Singh Chauhan <withshubh@gmail.com>
Co-authored-by: Adish M <adish.madhu@gmail.com>
Co-authored-by: Akshay <akshaysasidharan93@gmail.com>
2024-02-29 11:56:03 +05:30

3.8 KiB

id title
import-external-libraries-using-runjs Import external libraries using RunJS

ToolJet allows you to integrate external JavaScript libraries into your application using RunJS queries. This guide walks you through the process of importing and utilizing these libraries effectively.

Choosing Libraries

You can import various JavaScript libraries using their Content Delivery Network (CDN) links. Find the CDN links for your desired open-source projects on jsDelivr.

Creating a new app and runJS query

Start by creating a new application in ToolJet. Then, proceed to create a new RunJS query from the query panel.

reate a new RunJS query

Importing Libraries

Here's a step-by-step guide to importing libraries and displaying an alert upon successful import.

// Function to add script dynamically
function addScript(src) {
    return new Promise((resolve, reject) => {
        const scriptTag = document.createElement('script');
        scriptTag.setAttribute('src', src);
        scriptTag.addEventListener('load', resolve);
        scriptTag.addEventListener('error', reject);
        document.body.appendChild(scriptTag);
    });
}

try {
    // Importing MathJS
    await addScript('https://cdn.jsdelivr.net/npm/mathjs@11.7.0');

    // Importing FlattenJS
    await addScript('https://cdn.jsdelivr.net/npm/flattenjs@2.1.3/lib/flatten.min.js');

    // Showing a success alert
    await actions.showAlert("success", 'Mathjs and Flatten imported');
} catch (error) {
    console.error(error);
}

After creating and running the query, an alert should pop up with the message "Mathjs and Flatten imported."

:::tip Enable the Run this query on application load? option to make the libraries available throughout the application as soon as the app is loaded. :::

reate a new RunJS query

Examples

1. Flattening JSON Objects using FlattenJS

Create a new RunJS query using the Flatten library (imported earlier) to flatten a JSON object.

return flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
});

Preview the output in the query manager or run the query to see the flattened JSON.

reate a new RunJS query

2. Computation using MathJS

Create another RunJS query utilizing the MathJS library for a calculation.

return math.atan2(3, -3) / math.pi;

Preview the output, or Run the query to see the result of the calculation.

reate a new RunJS query

This guide provides a clear and detailed walkthrough for importing external JavaScript libraries into your ToolJet application.