I am new zk framework. In my zul file there is a notification count indicator, which is a property of view model and indicates the notification count. When I click on notification indicator it opens a toggle window containing notifications. Here I want to implement that when I read a notification , the count will be reduced by one.
<div class="notification_popup" viewModel="@id('vm') @init('com.zk.viewmodels.ViewAnnouncementViewModel')"> <a sclass="activity-dropdown" id="announcement_notification" iconSclass="z-icon-bell" popup="ann_notification,position=after_end,type=toggle" tooltiptext="Notifications"> <span class="num"><label value="@bind(vm.announcementCount)"/></span></a> <popup id="ann_notification" class="header-top-dropdown notification-dropdown"> <vlayout id="vl" sclass="notify-popup"> </vlayout> </popup> </div>
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class ViewAnnouncementViewModel {
private long announcementCount = 0;
@NotifyChange({".", "announcementCount"})
public void setAnnouncementCount(long announcementCount) {
this.announcementCount = announcementCount;
}
@SuppressWarnings("unchecked")
@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
super.afterCompose(view);
Selectors.wireComponents(view,this,false);
Html h1 = new Html();
h1.setContent("<h4>Notifications</h4>");
vl.appendChild(h1);
div = new Div();
div.setId("announcementList");
div = updateAnnouncement(div);
vl.appendChild(div);
}
public Div updateAnnouncement(Div div){
private Collection<AnnouncementResultDTO> searchResults =
announcementService.retrieveAnnouncement(instanceInfo);
announcementCount = searchResults.size();
setAnnouncementCount(announcementCount);
postNotifyChange(this,"announcementCount");
for(final AnnouncementResultDTO pasrDTO:searchResults){
A s1 = new A();
Label l1 = new Label();
l1.setValue("Annoncements");
s1.appendChild(l1);
div.appendChild(s1);
s1.addEventListener("onClick", new EventListener() {
public void onEvent(Event event) throws Exception {
Map<String, Object> map = new HashMap<String,Object>();
announcementCount=announcementCount-1;
setAnnouncementCount(announcementCount);
postNotifyChange(this,"*");
Window window = (Window) Executions.getCurrent().createComponents("announcement_popup.zul",null,map);
window.doModal();
}
});
}
}
But announcementCount is reducing when event fires but it is not reflected in view model. I think the problem is that if we debug and try to find the value 'this', it will contain object with two inner object , one is view model. Here we can understand that postnotify method receives not a view model, but a wrapper class which contain viewmodel as inner class. I dont know my assumption is true. Please help me
I got the solution for this . Use postNotifyChange(ViewAnnouncementViewModel.this,""); inside onEvent() method instead of postNotifyChange(this,"");
s1.addEventListener("onClick", new EventListener() {
public void onEvent(Event event) throws Exception {
Map<String, Object> map = new HashMap<String,Object>();
announcementCount=announcementCount-1;
setAnnouncementCount(announcementCount);
postNotifyChange(ViewAnnouncementViewModel.this,"*");
Window window = (Window) Executions.getCurrent().createComponents("announcement_popup.zul",null,map);
window.doModal();
}
});
Thanks https://www.javacodegeeks.com/2012/07/zk-in-action-mvvm-working-together-with.html