0 votes
2.1k views
by (140 points)
Hi,

Often, I have a function that I want to be able to use in multiple Client-side scripts, in multiple Human Services.

Examples include generic validation functions, date or JSON manipulation...etc

In Coach views, it's easy to have a common JS file with all the functions, and include it in the "included scripts" of the coach view. But how do I accomplish the same in Client-Side Human Services?

1 Answer

0 votes
by (30.6k points)

Put the shared functions in a managed web asset (a .js file in the toolkit) and include it in one small coach view that every human service drops on its coach - the functions become globals of the coach page, the client-side scripts of the CSHS can call them.

  1. Toolkit > Web files > upload common.js:
// common.js (managed web asset) - one namespace, no globals leaking
window.myco = window.myco || {};
myco.isEmail = function (s) { return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(s || ""); };
myco.addDays  = function (d, n) { var r = new Date(d); r.setDate(r.getDate() + n); return r; };
myco.deepGet  = function (o, path) { return path.split(".").reduce(function (x, k) { return x == null ? x : x[k]; }, o); };
  1. Coach view Common Library (no binding, no HTML): Behavior > Included Scripts: add common.js. Put this view once on every coach (or into a header coach view you already use).
  2. Any client-side script or control event of the CSHS can now use myco.isEmail(${Email}.getData()). Coach views load their included scripts once per page, so the functions are available from the coach's load event onwards.

Server-side scripts (service flows) cannot include a JS file; there the pattern is a toolkit service per function, or - the pragmatic one - a script that defines the functions stored in an environment variable / EPV and evaluated with eval(tw.env.commonJs) (works, keep it small), or a Java integration with the logic in Java.

Keep the library in a toolkit so that every process app gets the same version, and version the file name (common-1.2.js) when you change signatures.

References

Related questions

0 votes
1 answer 2.9k views
0 votes
1 answer 1.4k views
0 votes
1 answer 1.3k views
0 votes
1 answer 2.9k views
0 votes
1 answer 3.4k views
0 votes
2 answers 10.2k views
–1 vote
1 answer 3.0k views
asked Dec 5, 2019 in Toolkits by taurora (530 points)

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
...