DataWeb Script classes are files that can contain function
definitions and variable declarations. Depending on
how you define a function, you can call it from another
function in the same class, from script running in a
different class, or from the URL.
|Building DataWeb Script Classes
a New Script Class File
To create a new script class file, select the folder
in which you want to create the file in the DataWeb
Designer, then click the New button. Select the Create
New Script File option, then enter a name for the new
file. The file will be created with the .ws extension
and displayed in the code window.
Script to a Script Class
You can define functions and declare variables within
a script class. The functions and variables in a class
are also called members of that class. Although you
can define whatever members you want in a script class,
it's a good idea to group members in a class around
a similar theme, to keep your code well-organized. For
example, if you're building a Circle class, you might
include members like Radius, Circumference, and Area,
but put the Hypotenuse member in the Triangle class.
A function definition or a class-level variable declaration
in a script class can include attributes which specify
its scope - that is, which other scripts can call the
function or use the variable. The scoping attributes
for a function or variable in a script class are described
in the following table:
||A function defined as webcall can be called from any script in the web or from the URL.
||Default for functions. A function defined as public can be called from any script in the web that has permissions to import the class, but not from the URL.
||A variable declared as public can be used from any script within the web.|
||A function defined as private can be called only by other functions within the same class.
||Default for variables. A variable declared as private can be used only by functions within the same class.|
By default, functions in a class are public, and variables are private.
To call a function in a script file from another script file or from a view, use the import() method of the Classes object, as shown in the following example:
#var c = classes.import("/SomeClass.ws");
c.myFunction(arg1, arg2, arg3);#
See Calling Functions for more information on calling functions from within .view files.
If the function is defined as webcall, then you can also call it from the URL. To call a function in a script class from the URL,
include the path to the .ws file that contains the function. Pass the name of the function on the URL as the _fn parameter, and specify any arguments
by name, along with their values, as follows:
A Simple Example
Assume you have the following function, Sum(a, b, c), stored in the file http://myApplication.DataWeb.com/ScriptClass.ws.
webcall function Sum(a, b, c)
var product = Number(a) + Number(b) + Number(c);
To output the sum of 1, 2, and 3 directly to a .view file, place the following function call within the .view file:
#var c = classes.import("/ScriptClass.ws");
c.Sum(1, 2, 3);#
You can find the sum of the values 1, 2, and 3 by calling the following URL:
A More Complex Example
You can create a script class that performs some common mathematical operations, such as calculating the average
or the median of a set of numbers. The following functions show how you might define these in a class named CommonMath.ws:
/* CommonMath.ws */
/* This function can be called only from
a function defined in this class. */
private function calc_average(arr)
var sum = 0;
var num = arr.length;
for(var i = 0; i < num; i++)
var n = new Number(arr[i]);
//if value of arr[i] is not numeric,
//n is undefined.
sum += n;
throw("Please pass in only numbers.");
/* This function can be called from the url
or from other script.
When this function is passed a string of
comma separated values, it converts the
string into an array, and passes the array to the
function defined above, 'calc_average' */
webcall function average(args)
//if argument is a string,
//split string to create an array.
if(typeof args == "string")
//Split string argument into
//individual array elements.
var arr = args.split(",");
You could call the average()
function from the url as follows: