Embedded ZK Application"

From Documentation
Line 55: Line 55:
 
The "domId" means after loading resource from "ZKSrc", the content of "domId" (HTML DOM Element) would be replaced with the ZK content.
 
The "domId" means after loading resource from "ZKSrc", the content of "domId" (HTML DOM Element) would be replaced with the ZK content.
  
This function return a [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise Promise object], which means that we can call functions after ZK is ready.
+
This function returns a [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise Promise object], which means that we can call functions after ZK is ready.
  
 
<syntax lang="javascript">
 
<syntax lang="javascript">

Revision as of 09:14, 20 April 2021


Embedded ZK Application


Employment/Purpose

Instead of using iframe, there is a new way to use ZK in a non-ZK web container. For example, we could use NodeJs, Python, etc. as the web application, and embed another ZK application in the web pages.

  • Available for ZK:
  • http://www.zkoss.org/product/zkhttp://www.zkoss.org/whyzk/zkeeVersion ee.png

Since 9.0.1

Prerequisite

Settings in the ZK application

We use library property to enable the embedded feature. For example: (In zk.xml)

<library-property>
	<name>org.zkoss.web.servlet.http.embedded.enabled</name>
	<value>true</value>
</library-property>

Demo Example

In the Demo project, there are 2 web applications, one is a non-ZK web application (http://localhost:8080), and another one is a ZK application (http://zkembedded-app).

We'll embed ZK into the non-ZK web application.

index.html <syntax lang="html" high="11,13"> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body>

Loading...

<script id="embeddedScript" src="http://zkembedded-app/zkau/web/js/zkmax/embedded/embedded.js" /> <script> zEmbedded.load('embeddedZK', 'http://zkembedded-app/demo.zul'); </script> </body> </html> </syntax>

  • line 11: load the zk embedded JS API script
  • line 13: load demo.zul (of the ZK application) into the DOM Element with id embeddedZK.

To see more information, please download the Demo project.

API in embedded.js

We provide two methods to embed ZK.

zEmbedded.load(domId, ZKSrc)

The "domId" means after loading resource from "ZKSrc", the content of "domId" (HTML DOM Element) would be replaced with the ZK content.

This function returns a Promise object, which means that we can call functions after ZK is ready.

<syntax lang="javascript"> zEmbedded.load('embeddedZK', 'http://zkembedded-app/demo.zul')

 .then(function(result) {
   zk.log('ZK is ready!' + result.widget.uuid); //result contains the first widget
 }).catch(reason => {
   alert('ZK mounting error: ' + reason);

}); </syntax>

The "then" method of the promise is invoked when the embedding has completed, and the embedded page has been loaded.

The "catch" method of the promise is invoked when the embedding process encounters a failure. The reason object contains the error message associated with the failure.

zEmbedded.destroy(domId, skipError)

This will destroy the embedded ZK desktop at server side and clear the DOM Element. Use "skipError = true" to ignore error messages.

Control ZK Components when using embedded ZK

After loading the ZK contents, we could use the ZK Client binding to control the ZK components in the view.

To see more information, please refer to ZK MVVM Book - Client Binding.

Cross-Origin Resource Sharing

In Cross Origin scenarios the responses from the ZK application need to set at least the following CORS headers:

<syntax lang="javascript"> Access-Control-Allow-Origin: [allowed embedding origins] Access-Control-Allow-Headers: zk-sid Access-Control-Expose-Headers: zk-sid, zk-error Access-Control-Allow-Credentials: true Access-Control-Allow-Methods: GET, POST </syntax>

This can be done by configuring your server (e.g. nginx, apache-httpd, tomcat, spring-boot...) appropriately and is not ZK-specific. Please refer to the related documentation (e.g. MDN: Cross-Origin Resource Sharing (CORS)) and your specific server configuration guides.

Version History

Last Update : 2021/04/20


Version Date Content
9.0.0



Last Update : 2021/04/20

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.