Using Custom Javascript

Applications developed with JBStrap normally do not require the use of additional JavaScript solutions, as the framework provides complete solutions, even for the client side. However, you may need to implement specific custom solutions, for which you will need to use JavaScript. Use of additional, custom JavaScript is supported by the framework, there are multiple ways to execute JavaScript code. You can send the client instructions from the server-side code, create your own JavaScript library or you can create your own component, using a third-party JavaScript library. Regardless, the JavaScript error handler method, provided by the framework, will be fully functional. To learn more, visit the a Testing Client-side code section.

The framework also supports the use of JQuery , as the 3.1.1 version of JQuery is loaded by default on the interface. Click here to learn more about JQuery 3.1.1 .

Running custom JavaScript statements

You can issue JavaScript statements at any point of the Application. You can issue them through a component or through a Client. If the statement is issued through a component, then it will be executed on the Client, on which the component is drawn. If the component is not displayed (not drawn, or not added to a page) then the JavaScript will not be executed, since it cannot be decided on which Client is the statement to be executed. Statements issued through the Client class will be always executed.

Executing JavaScript through a component:
Executing JavaScript through the Client class:

In the above examples, the text ’Hello world’ will appear in the browser log. These are sent to the client-side in a package after the thread was executed. For example, on a thread after an event has been processed and the end of the thread has been reached, the package with the JavaScript from every thread is placed on the interface, in the order they were issued. If you need to execute JavaScript immediately, you have to do it through the Client class:

Executing JavaScript instantly:

Statements issued like this do not wait for the thread, they are sent to the client and executed as soon as they were issued

If you want to use a statement, which has a return value that you need to use (e.g querying something on the client side, or using a JavaScript function that calculates something), you need to do this through the Client class:

Executing a JavaScipt statement that has a return value:

In the above example, you will get the inner width of the Client’s browser window, in pixels. This statement is executed instantly, and the execution of server-side code is suspended for the duration of this operation. The code on the server-side will behave as if a server-side getter method was called. If you do not get a reply with the return value in the specified time, the server-side code will resume, but the return value will be null. By default, this timeout is set to 30 seconds. This can be modified in the Client object, by calling the setTimeout method.

Setting the timeout of JavaScript functions:

In this example, the timeout was set to one minute. The timeout must be specified in milliseconds (1 second = 1000 milliseconds).

Using a custom JavaScript library

You can use third-party JavaScript libraries, or your own libraries with applications created with the JBStrap framework. These libraries and packages must be placed in the project’s WebContent folder, then loaded into the framework.

Loading a JavaScript library into the framework:

These loaded JavaScript packages will be automatically sent to the client when they connect, and they will be ready to use. Note however, that you cannot use JQuery functions in these custom packages, as the JQuery is loaded on every client before the JavaScript packages.

Calling the ’myJSFunction’ (located in the custom JS package) function from the server-side: