Put the functions in one JavaScript file as a managed web asset, expose them under a single namespace, and include the file from one coach view that every coach carries - then every client-side script and control event can call them (question 3123 shows the same for CSHS scripts). A structure that stays maintainable as the file grows:
// common.js (managed web file "common.js" or inside common.zip/js/common.js)
(function (ns) {
// validation
ns.isEmail = function (s) { return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(s || ""); };
ns.required = function (control, msg) { var ok = !!control.getData(); control.setValid(ok, ok ? "" : (msg || "Required")); return ok; };
// formatting
ns.money = function (n, cur) { return (cur || "EUR") + " " + (Math.round((n || 0) * 100) / 100).toFixed(2); };
ns.date = function (d) { d = new Date(d); return isNaN(d) ? "" : d.toISOString().slice(0, 10); };
// UI helpers
ns.notify = function (alertControl, style, text) { alertControl.setColorStyle(style); alertControl.setText(text); alertControl.setVisible(true); };
// REST helper (BPM REST API, CSRF aware)
ns.rest = function (method, path, body, cb) { /* see the answers on REST calls from coach views */ };
ns.version = "1.3.0";
})(window.myco = window.myco || {});- Coach view Common Library (no layout) > Behavior > Included Scripts: common.js. Add this view to a header view that all coaches use, so nobody forgets it.
- Use from anywhere: myco.required(${Email}, "E-mail is required"), myco.money(${Total}.getData()).
- Version the file name when you change signatures (common-1.3.js), keep the source in Git, minify in the build if it grows.
Server-side functions cannot share a file; there the equivalent is a toolkit service per function (or a Java helper class). Keep browser and server helpers separate - tw.local semantics differ.
References