-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have a data entry MVVM form which includes a 'save' button. If the user clicks on the 'save' button then the resulting MVVM class command method can run for as long as a minute. The process being executed in the MVVM class method has 9 steps.
What I'd like to do is have a progress message displayed in the MVVM window after each of the nine steps. But I can't find a way of having the window update itself.
Any suggestions?
I've tried using a timer but the timer just doesn't seem to get a chance to force a screeen refresh.
You need to do those steps in another thread, also pass vm to that thread so you can update properties of vm as needed, and use timer to update properties.
For more information, please refer to the simple sample below:
test.zul
<zk>
<div apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('test.TestVM')">
<button id="btn" label="do multiple steps long operation"
onClick="@command('doLongSteps')" />
<label value="@load(vm.msg)" />
<timer running="@load(vm.isRunning)" repeats="true"
delay="500" onTimer="@command('checkStatus')" />
</div>
</zk>
TestVM.java
package test;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.util.Clients;
public class TestVM {
String _msg;
boolean _isRunning;
public void setMsg (String msg) {
_msg = msg;
}
public String getMsg () {
return _msg;
}
public void setIsRunning (boolean isRunning) {
_isRunning = isRunning;
}
public boolean getIsRunning () {
return _isRunning;
}
@Command
@NotifyChange({"isRunning", "msg"})
public void doLongSteps () {
_isRunning = true;
new Thread(new MultiStepsLongOperationWorker(this)).start();
}
@Command
@NotifyChange({"isRunning", "msg"})
public void checkStatus () {
if (_isRunning) {
Clients.showBusy(_msg);
} else {
Clients.clearBusy();
}
}
}
MultiStepsLongOperationWorker.java
package test;
import java.util.Random;
public class MultiStepsLongOperationWorker implements Runnable {
private TestVM _vm;
public MultiStepsLongOperationWorker (TestVM vm) {
_vm = vm;
}
public void run () {
int step = 1;
Random r = new Random();
while (step <= 9) {
_vm.setMsg("Processing step " + step + "...");
try {
Thread.sleep(r.nextInt(2000) + 1000);
} catch (Exception e) {
e.printStackTrace();
}
step++;
}
_vm.setIsRunning(false);
}
}
Asked: 2013-03-25 22:01:42 +0800
Seen: 89 times
Last updated: Mar 27 '13
Databinding and auto-complete on combobox
Composite component and bind in ZK 6
How to detach / reattach MVVM windows?
Is there a way to resolve view model properties as input to client side javascripts?
How can I synchronize data in a ListBox in MVVM ? [closed]
MVVM Validator: class not found ? [closed]
How to Call Child ViewModel Method from Parent Window? [closed]