Common Script

Introducing Common Script

Common script is a kind of script library that collects frequently used functions in the project. Scripts that are required only for a specific form are written in form files, but it is recommended that scripts used throughout the application be written in a separate file (XJS) and referenced in each form.

XJS is the extension of the script file used in Nexacro.

If you create the common script once, then you can avoid the duplication of developing the same function multiple times, and it is efficient because unified functions can be used when multiple people work. Also, the common script file can be used in other projects as-is, resulting in reusing the code and reducing the development time. However, unnecessary abuse can increase the size of the application, causing resource wasting and speed delay; therefore, it is recommended to use it only where it is needed.

Generally, the functions implemented with the common script can be any functions commonly used such as character, date check, string processing, format verification, various operations, encoding/decoding, and communication connection, and the contents may vary depending on the project.

Writing XJS

Common script is written in the XJS file. You can create the necessary functions after creating the XJS file in the project to use them by referring to them in each form. The process of writing XJS is as follows.

1

Select [File > New > Script (.xjs)] from the menu to open the New Script pop-up window.

New_Script_popup_02

2

Set libCommon for Name and Base for Location, which is the default value, and click the OK button. Then, the XJS file is created and opened automatically in the script editing window.

The created XJS file can be checked in Project Explorer as follows.

3

Write the script for the functions required for the XJS file opened in the script editing window.

When declaring the function, add the 'this' keyword. If you do not add 'this', then it is processed as global processing.

/* libCommon.xjs */
this.isNumber = function(str)
{
	var retVal = nexacro.isNumeric(str);
	return  retVal;
}

this.fn_add = function(x, y)
{
	var retAdd = nexacro.toNumber(x) + nexacro.toNumber(y);
	return retAdd;
}

Using XJS

If you want to use the common script in the form, then you can simply use it by declaring the include statement on the page.

1

Double-click the form in Project Explorer to open it, and declare the use of XJS with the include statement at the top of the script editing window.

include "Base::libCommon.xjs";

Write the include statement and write the service ID and file name of the XJS file in "". Also, make sure to add the semicolon (;) at the end of the code to indicate that the line has ended.

Multiple files can be referenced with include if there are many scripts that are commonly referenced. Also, if there are many files that are referenced, then you can refer to other code again within the script code. For example, you can configure the libCommon.xjs script file as follows. With this configuration, even if the number of files to be referenced increases, you can add functions by modifying only the libCommon.xjs file without touching the form file used.


/* libCommon.xjs */

include "Lib::libAuth.xjs";

include "Lib::libGrid.xjs";

include "SERVICE::libService.xjs";

include "Util::libUtil.xjs";

2

Call the function from the form script.

You can use the fn_add function defined in libCommon.xjs in the form script as follows.

/* calc.xfdl */

include "Base::libCommon.xjs";

this.btn_calc_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo)
{
	var result = this.fn_add(5, 2);	//result = 7
};

Importing XJS

You can import the previously created XJS file into the project and use it.

1

Copy the XJS file to the Base directory in the project path.

If you copy the XJS file to a path other than the Base directory, then it cannot be used in the project. To use the XJS file in the path other than the Base directory, you need to register the path in User Service. Please refer to the next chapter for more details.

2

Refresh Project Explorer and check if the copied XJS file is displayed under Base.

3

Include XJS in the form script and use it.

Registering Service

To use the XJS file in a separate path, register the path as the service.

1

Select [TypeDefinition > Services] of Project Explorer, right-click, and select the [Edit] item from the context menu to open the service editing window.

2

Click the + button in User Service to enter the service ID (PrefixID), file type (Type), and path (URL) information. Leave other items as default values.

3

Check the service path in Project Explorer.

When the new service is registered, the service and the XJS file located in the path are displayed in Project Explorer and can be managed.

Using Script in App

App (XADL) is the main part of the project and is called when the application is loaded. In the script code of the XADL file, the common script is defined and used as needed to create functions that are mainly needed when starting or ending the application.

When the common script is written in XADL, it is initially loaded with the application object. Therefore, there is no need for a separate include process, and if the object is defined as this object it operates as the child of the application object.

As index.html is automatically updated whenever the XADL file is changed, you need to manage index.html separately if you coded additionally.

1

Select App '[Project Name]' of the project in Project Explorer, and right-click to select [Edit Script] from the context menu.

2

When the XADL file is opened in the script editing window, implement the functions as follows.

/* calc.xadl */

this.adl_add = function(x, y)
{
	var retAdd = nexacro.toNumber(x) + nexacro.toNumber(y);
	return retAdd;
}

3

Use the function defined in XADL in the form.

You can use the adl_add function defined in calc.xadl as follows after obtaining the application object using the nexacro.getApplication method.

/* calc_adl.xfdl */

var objApp = nexacro.getApplication();
result = objApp.adl_add(5, 2);	//result = 7