Showing posts with label ZK. Show all posts
Showing posts with label ZK. Show all posts

2012/12/19

ZK - 好用的 Command 別名


ViewModel
@Command({"delete", "deleteOrder"})
public void deleteOrder(){
      //delete order
}

View
<button onclick="@command('delete')"/>
<button onclick="@command('deleteOrder')"/>

參考
http://books.zkoss.org/wiki/ZK%20Developer's%20Reference/MVVM/ViewModel/Commands

2012/12/13

ZK - IllegalStateException: UI can't be modified in the rendering phase

Situation:
index.zul
<?page id="foo"?>
<zk>
 <include id="header" mode="defer" src="/template/header.zul" />
 <include id="mainView" mode="defer" src="/discussion/list.zul" />
</zk>

list.zul
<div sclass="container" vflex="9" apply="org.zkoss.bind.BindComposer"
 viewModel="@id('vm') @init('com.vm.DiscussionListViewModel')">
 ........
 <button onClick="@command('Change')"/>
</div>

DiscussionListViewModel.java
@Command
public void buttonClick!() {
 Include i = (Include) Path.getComponent("//foo/mainView");
 i.setSrc("change.zul");
}
It will occur UI can't be modified problem, because of composer died before event finished.
You cannot change "include component" by the composers which are included in the "include component"

Rendering Phase:

The Rendering PhaseAfter all events are processed, ZK renders these components into a regular HTML page and sends this page to the browser.

Reference: [1]http://books.zkoss.org/wiki/ZK_Developer's_Guide/Fundamental_ZK/Basic_Concepts/Architecture_Overview

2012/12/12

ZK 取得特定 Page 內的所有 Component

index.zul
<?page id="foo"?>
<zk>
 <include id="header" src="/header.zul" />
 <include id="mainView" src="/main.zul" />
</zk>

ViewModel or Composer
Method 1. Page Access
Page p = Executions.getCurrent().getDesktop().getPage("foo");

Collection<Component> c = p.getRoots();
for (Iterator<Component> it = c.iterator(); it.hasNext();) {
 Component cc = it.next();
 log.info(cc.getClass().toString());
}

Result: 印出兩個 Include Component。
Method 2. Path Access
Include i = (Include) Path.getComponent("//foo/mainView");
i.setSrc("/change.zul");

兩個 / 代表 Page ID,一個代表 Component ID

2012/11/28

ZK - Window Component Close 注意事項

Title 屬性很重要,會影響到是否可以關閉 or 移動視窗...etc

<window id="loginWin" title= "Login" visible="false" closable="true" border="normal"/>

如果沒有 title 的話 closable 會失效。
如果想要 Window 能在關上後重複打開,需要自己 override onClose 屬性
onClose="self.setVisible(false); event.stopPropagation()"
event.stopPropagation() 可以停止事件把 Window 物件拋棄掉。

ZK - 用 Include Component + Button Component 進行 Ajax 換頁

首先先是 View 的部分


index.zul
<button id="btnChange" label="Change!!"></button>
<include id="mainView" src="content.zul"></include>

接下來是相對應的 Composer

IndexComposer.java
public class IndexComposer extends GenericForwardComposer<Component> {

  private static final long serialVersionUID = 1L;
  
  @Wire("#mainView")
  private Include mainView;
  
  public void onClick$btnChange() {
   mainView.invalidate();
   mainView.setSrc("changed.zul");
  }
 
}