Install and uninstall
Prepare data when CMS installs or loads a widget and clean up when it is removed.
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
| Event | Hooks |
|---|---|
| The widget is saved for the first time (Create, Import or a file) | install |
| CMS starts or reloads its data, for example after a restore | install 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 removed | uninstall |
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:
| Property | What it holds |
|---|---|
db.vars | The variables, as an object of keys and values. |
db.config | The settings of the site. |
db.storage | An 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.