2016-01-29 19 views
0

我加入這個行爲到組件(MarkupContainer)後更新Wicket的AjaxSelfUpdatingTimerBehavior停止離開並重新進入頁面

AjaxSelfUpdatingTimerBehavior updateBehavior = new AjaxSelfUpdatingTimerBehavior(Duration.seconds(3)) 
    { 


     @Override 
     public void onEvent(Component component, IEvent<?> event) { 
      // some business logic 
     } 

    }; 

某處,在同一頁上我有一個AjaxLink其重定向到另一個頁面(在誰構造函數我傳遞實際頁面作爲參數),並在該頁上我有一個「返回」AjaxLink重定向我,調用setResponsePage(myFirstPage)

問題是,即使在渲染頁面時行爲更新一次,它在3秒鐘停止更新一次,如同構造的。在離開頁面之前沒有問題面對行爲。

回答

0

可能不是最好的解決方案,但我設法通過刪除頁面的行爲onBeforeRender()並再次添加來解決它。我在頁面上聲明瞭一個字段private int autoUpdateBehaviorId = -1;

public void addUpdateBehavior(Component c) 
{ 
    if(autoUpdateBehaviorId >= 0) 
     c.remove(c.getBehaviorById(autoUpdateBehaviorId)); 


    AjaxSelfUpdatingTimerBehavior updateBehavior = new AjaxSelfUpdatingTimerBehavior(Duration.seconds(3)) 
    { 

     @Override 
     public void onEvent(Component component, IEvent<?> event) { 
      // bussines logic 
     } 
    }; 
    c.add(updateBehavior); 
    autoUpdateBehaviorId = c.getBehaviorId(updateBehavior); 
} 


@Override 
protected void onBeforeRender() { 
    super.onBeforeRender(); 
    addUpdateBehavior(myContainer); 
} 
+0

我沒有看到沒有理由,爲什麼這不應該沒有workaroud工作。你可以創建一個快速入門並將其附加到Jira問題? – svenmeier

0

不一定是您的問題的解決方案;但是我已經通過覆蓋AjaxSelfUpdatingTimerBehavior的onConfigure方法來實現該行爲,如下所示。

在我的情況下,我不得不每10秒更新一次標籤,並在隊列中記錄當前記錄的數量。

以下是代碼片段:

labelToBeUpdated.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(configurableDelay)) { 
     @Override 
     public void onConfigure(Component component) { 
      String inProgressOutOfTotal = "10/100"; //Business logic to get total count and inprogress count  
      labelToBeUpdated.setDefaultModel(Model.of(inProgressOutOfTotal)); 

      //Set visibility of the component if needed 
     } 
    } 
    labelToBeUpdated.setOutputMarkupId(true); 

只是好奇;是否onEvent正在等待組件上的事件以便刷新?由於onConfigure在渲染週期開始之前被調用,它正在爲我工​​作。

但正如Sven Meier所說,你可能仍然想要在他的建議下使用onEvent獲得你的代碼。

相關問題