Inter-Page Communication"
Line 2: | Line 2: | ||
=Inter-Page Communication= | =Inter-Page Communication= | ||
+ | |||
+ | 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 <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()" 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" > | ||
+ | comp.setAttribute("some","anyObject"); | ||
+ | comp.getAttribute("some", comp.DESKTOP_SCOPE); | ||
+ | comp.getDesktop().getAttribute("some"); //is equivalent to previous line | ||
+ | </source> | ||
+ | |||
+ | Relevant api includes <tt>setAttribute</tt>, <tt>getAttribute</tt>, <tt>removeAttribute</tt>. | ||
+ | |||
+ | In <tt>zscript</tt>, you can use call such api through implicit objects. For example, | ||
+ | <source lang="xml" > | ||
+ | <window> | ||
+ | <zscript><![CDATA[ | ||
+ | desktop.setAttribute("some","anyObject"); | ||
+ | ]]> | ||
+ | </zscript> | ||
+ | 1:${desktopScope["some"]} | ||
+ | </window> | ||
+ | </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 == | ||
+ | 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. | ||
+ | |||
+ | <source lang="java" > | ||
+ | Events.postEvent(new Event("SomethingHappens", | ||
+ | comp.getDesktop().getPage("another").getFellow("main")); | ||
+ | </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". | ||
+ | |||
+ | <source lang="xml" > | ||
+ | <window use="MyWindow"> | ||
+ | <zscript> | ||
+ | Events.postEvent("onTest", self, "this will be send"); | ||
+ | </zscript> | ||
+ | </window> | ||
+ | </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= | =Inter-Application Communication= | ||
=Use Cookie= | =Use Cookie= |
Revision as of 10:13, 25 November 2010
Inter-Page Communication
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());
}
}
Inter-Application Communication
Use Cookie
Version History
Version | Date | Content |
---|---|---|