Inter-Page Communication

From Documentation
Revision as of 06:31, 15 July 2010 by Maya001122 (talk | contribs) (Created page with '{{ZKDevelopersGuidePageHeader}} __TOC__ Communications among pages in the same desktop is straightforward. First, you can use attributes to share data. Second, you can use even…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


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.

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 zscript and EL expressions, you could use the implicit objects: componentScope, pageScope, desktopScope, sessionScope, requestScope and applicationScope.

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.)

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

Relevant api includes setAttribute, getAttribute, removeAttribute.

In zscript, you can use call such api through implicit objects. For example,

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

Note that desktop setAttribute will be saved at desktopScope. In EL, you can use "[ ]" operator as the key to retrieve element of map.

Post and Send Events

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

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

You can also pass object by event. The third parameter in postEvent will be put into Event.data. You can encapsulate data you want to pass in the object. In the following example, window's title will be set to "this will be send".

<window use="MyWindow">
    <zscript>    
    Events.postEvent("onTest", self, "this will be send");    
    </zscript>    
</window>

And MyWindow.java is as follow,

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());
	}
}

Note: These api has to take care about component life cycle, if you use these api outside ZK event handler, please refer to server push.



Last Update : 2010/07/15

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