+1 vote
689 views
in Best Practices by (21.5k points)

1 Answer

0 votes
by (30.6k points)

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 || {});
  1. 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.
  2. Use from anywhere: myco.required(${Email}, "E-mail is required"), myco.money(${Total}.getData()).
  3. 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

Related questions

0 votes
1 answer 760 views
0 votes
1 answer 1.6k views
0 votes
1 answer 2.8k views
0 votes
1 answer 600 views
0 votes
1 answer 2.5k views
0 votes
1 answer 3.3k views
asked Oct 21, 2018 in BPD by BPM Tips Admin (21.5k points)
+1 vote
1 answer 1.8k views
0 votes
1 answer 1.0k views
0 votes
2 answers 2.9k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...