Spanish Total.js
English
Documentation For AI agents
Contents

Install and uninstall

Prepare data when CMS installs or loads a widget and clean up when it is removed.

.md GitHub
On this page

exports.install and exports.uninstall run on the server and receive the CMS data. Use them to prepare what the widget needs, such as a variable with a default value.

An example

Support line shows an e-mail address from the variable supportemail. When the widget is installed, it creates the variable if it does not exist:

<script total>

    exports.id = 'supportline';
    exports.name = 'Support line';
    exports.author = 'Jane Doe';
    exports.version = '1';

    exports.install = function(db) {
        if (!db.vars.supportemail)
            db.vars.supportemail = 'help@example.com';
    };

    exports.uninstall = function(db) {
        console.log('Support line: uninstall');
    };

</script>

<style>
    .CLASS-box { padding: 10px 15px; border-left: 4px solid #2F9E6E; }
</style>

<body>
    <div class="CLASS-box"><span class="CMS_edit">Need help? Write to</span> <a href="mailto:$supportemail" class="CMS_edit">$supportemail</a></div>
</body>

On the public page the visitor sees help@example.com, and the user changes it on the Variables screen.

When the hooks run

EventHooks
The widget is saved for the first time (Create, Import or a file)install
CMS starts or reloads its data, for example after a restoreinstall for each widget
The widget is saved again (Edit, or Import of an installed widget)uninstall, then install of the new code
The widget is removeduninstall

Because install runs at every start, make it safe to run many times: check before you create something, as the example does. Because uninstall also runs before every update, do not delete data there that the user must keep, such as the value of a variable.

The data

Both functions receive the CMS data as their argument (and as this). These properties are useful in hooks:

PropertyWhat it holds
db.varsThe variables, as an object of keys and values.
db.configThe settings of the site.
db.storageAn object stored with the data, free for your own values.

CMS saves the data after it installs, updates or removes a widget. An error in a hook is written to the console of the server and does not stop the others.

Made in Europe