Inter-Page Communication"

From Documentation
m (remove empty version history (via JWB))
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
  
=Inter-Page Communication=
+
__TOC__
 +
Communicating among pages in the same desktop is straightforward. First, you can use attributes to share data. Second, you can use events to notify each other.
  
Communications among pages in the same desktop is straightforward. First, you can use attributes to share data. Second, you can use event to notify each other.
+
= Identify a Page=
  
== Attributes ==
+
To communicate among pages, we have to assign an identifier to the target page. In ZUML, it is done by the use of [[ZUML Reference/ZUML/Processing Instructions/page|the page directive]]:
 +
 
 +
<source lang="xml">
 +
<?page id=" foo"?>
 +
<window id="main"/>
 +
</source>
 +
 
 +
Then we could retrieve it by use of <javadoc method="getPage(java.lang.String)" type="interface">org.zkoss.zk.ui.Desktop</javadoc> or by use of a utility class called <javadoc>org.zkoss.zk.ui.Path</javadoc>. For example, the following statements could access the ''main'' window above:
 +
 
 +
<source lang="java">
 +
comp.getDesktop().getPage("foo").getFellow("main");
 +
Path.getComponent("//foo/main");
 +
</source>
 +
 
 +
As shown, <javadoc method="getComponent(java.lang.String)">org.zkoss.zk.ui.Path</javadoc> considers an ID starting with double slashes as a page's ID.
 +
 
 +
= Use Attributes =
 
Each component, page, desktop, session and Web application has an independent map of attributes. It is a good place to share data among components, pages, desktops and even sessions.
 
Each component, page, desktop, session and Web application has an independent map of attributes. It is a good place to share data among components, pages, desktops and even sessions.
  
In <tt>zscript</tt> and EL expressions, you could use the implicit objects: <tt>componentScope</tt>, <tt>pageScope</tt>, <tt>desktopScope</tt>, <tt>sessionScope, requestScope</tt> and <tt>applicationScope</tt>.
+
In Java , you could use "setAttribute()","removeAttribute()" and "getAttribute()" of <javadoc type="interface">org.zkoss.zk.ui.Component</javadoc>, <javadoc type="interface">org.zkoss.zk.ui.Page</javadoc> and so on to share data. Another way is  using the scope argument to identify which scope you want to access. (In the following example, assuming comp is a component.)
 
 
In Java , you could use "setAttribute()","removeAttribute()" and "getAttribute()" to share data. Another way is  using the scope argument to identify which scope you want to access. (In the following example, assuming comp is a component.)
 
  
 
<source lang="java" >
 
<source lang="java" >
Line 18: Line 33:
 
</source>
 
</source>
  
Relevant api includes <tt>setAttribute</tt>, <tt>getAttribute</tt>, <tt>removeAttribute</tt>.  
+
In <code>zscript</code> and EL expressions, you could use the [[ZUML Reference/EL Expressions/Implicit Objects|implicit objects]]: <code>componentScope</code>, <code>pageScope</code>, <code>desktopScope</code>, <code>sessionScope, requestScope</code> and <code>applicationScope</code>.
  
In <tt>zscript</tt>, you can use call such api through implicit objects. For example,
 
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
<zscript><![CDATA[  
+
    <zscript><![CDATA[
        desktop.setAttribute("some","anyObject");         
+
        desktop.setAttribute("some","anyObject");
]]>
+
         desktopScope.get("some");
</zscript>
+
    ]]></zscript>
1:${desktopScope["some"]}
+
    1:${desktopScope["some"]}
 
</window>
 
</window>
 
</source>
 
</source>
  
Note that <tt>desktop</tt> <tt>setAttribute</tt> will be saved at <tt>desktopScope</tt>. In EL, you can use "[ ]" operator as the key to retrieve element of map.
 
  
== Post and Send Events ==
+
= Post and Send Events =
You could communicate among pages in the same desktop. The way to communicate is to use the <tt>postEvent</tt> or <tt>sendEvent</tt> to notify a component in the target page.
+
You could communicate among pages in the same desktop. The way to communicate is to use the <javadoc method="postEvent(org.zkoss.zk.ui.event.Event)">org.zkoss.zk.ui.event.Events</javadoc> or <javadoc method="sendEvent(org.zkoss.zk.ui.event.Event)">org.zkoss.zk.ui.event.Events</javadoc> to notify a component in the target page.
  
 +
For example,
 
<source lang="java" >
 
<source lang="java" >
 
  Events.postEvent(new Event("SomethingHappens",
 
  Events.postEvent(new Event("SomethingHappens",
     comp.getDesktop().getPage("another").getFellow("main"));
+
     comp.getDesktop().getPage("foo").getFellow("main"));
 
</source>
 
</source>
  
You can also pass object by <tt>event</tt>. The third parameter in <tt>postEvent</tt> will be put into <tt>Event.data</tt>. You can encapsulate data you want to pass in the object. In the following example, <tt>window</tt>'s title will be set to "this will be send".
+
You can also pass the data with the event object. The third parameter in <javadoc method="postEvent(org.zkoss.zk.ui.event.Event)">org.zkoss.zk.ui.event.Events</javadoc> will be put into <javadoc method="getData()">org.zkoss.zk.ui.event.Event</javadoc>. You could pass the data you want with it.
  
<source lang="xml" >
+
<source lang="java" >
<window use="MyWindow">
+
Events.postEvent("onTest", target, "this will be send");     
    <zscript>   
 
    Events.postEvent("onTest", self, "this will be send");     
 
    </zscript>   
 
</window>
 
 
</source>
 
</source>
  
And MyWindow.java is as follow,
 
<source lang="java" >
 
import org.zkoss.zk.ui.event.Event;
 
import org.zkoss.zul.Window;
 
 
public class MyWindow extends Window {
 
public void onTest(Event evt){
 
this.setTitle(evt.getData().toString());
 
}
 
}
 
</source>
 
  
=Inter-Application Communication=
 
=Use Cookie=
 
=Version History=
 
{{LastUpdated}}
 
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|}
 
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 05:54, 6 February 2024


Inter-Page Communication


Communicating among pages in the same desktop is straightforward. First, you can use attributes to share data. Second, you can use events to notify each other.

Identify a Page

To communicate among pages, we have to assign an identifier to the target page. In ZUML, it is done by the use of the page directive:

<?page id=" foo"?>
<window id="main"/>

Then we could retrieve it by use of Desktop.getPage(String) or by use of a utility class called Path. For example, the following statements could access the main window above:

comp.getDesktop().getPage("foo").getFellow("main");
Path.getComponent("//foo/main");

As shown, Path.getComponent(String) considers an ID starting with double slashes as a page's ID.

Use Attributes

Each component, page, desktop, session and Web application has an independent map of attributes. It is a good place to share data among components, pages, desktops and even sessions.

In Java , you could use "setAttribute()","removeAttribute()" and "getAttribute()" of Component, Page and so on to share data. Another way is using the scope argument to identify which scope you want to access. (In the following example, assuming comp is a component.)

 comp.setAttribute("some","anyObject");
 comp.getAttribute("some", comp.DESKTOP_SCOPE);
 comp.getDesktop().getAttribute("some"); //is equivalent to previous line

In zscript and EL expressions, you could use the implicit objects: componentScope, pageScope, desktopScope, sessionScope, requestScope and applicationScope.

<window>
    <zscript><![CDATA[
        desktop.setAttribute("some","anyObject");
        desktopScope.get("some");
    ]]></zscript>
    1:${desktopScope["some"]}
</window>


Post and Send Events

You could communicate among pages in the same desktop. The way to communicate is to use the Events.postEvent(Event) or Events.sendEvent(Event) to notify a component in the target page.

For example,

 Events.postEvent(new Event("SomethingHappens",
     comp.getDesktop().getPage("foo").getFellow("main"));

You can also pass the data with the event object. The third parameter in Events.postEvent(Event) will be put into Event.getData(). You could pass the data you want with it.

Events.postEvent("onTest", target, "this will be send");




Last Update : 2024/02/06

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