2011-06-27 4 views
1

私は現在のプロジェクトでそれを実装しようとしているので私はgwtの歴史をしばらく再生しているので、ちょうどそれが動作し、いくつかの練習を見るために小さなデモプロジェクトを作成しました。 なので、何らかの理由で動作しません。 ここにコードがあります - 非常に単純な前後です。ここで歴史GWTのデモ - 動作しません

はIFRAME

<iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe> 

、エントリポイント

public class EntryPoint implements com.google.gwt.core.client.EntryPoint { 

private FirstPanel panel; 

public void onModuleLoad() { 

    ContentPanel panel = ContentPanel.getInstance(); 
    panel.setContent(FirstPanel.getInstance()); 
    RootPanel.get().add(panel); 

} 
} 

3シングルトンフォーム、フォームがロードされているコンテンツ及び2形態です。

public class ContentPanel extends Composite 
{ 
private static ContentPanel instance; 
private static VerticalPanel panel = new VerticalPanel(); 
private ContentPanel() 
{ 
    initWidget(panel); 
} 

public void setContent(Widget widget) 
{ 
    panel.clear(); 
    panel.add(widget); 
} 
public static ContentPanel getInstance() 
{ 
    if(instance == null) 
    return instance = new ContentPanel(); 
    else 
    return instance; 
} 
} 

と..

public class FirstPanel extends Composite implements HistoryListener { 

private static FirstPanel instance; 
private VerticalPanel panel; 

public FirstPanel() { 

    History.addHistoryListener(this); 

    panel = new VerticalPanel(); 
    panel.setStyleName("panelstyle"); 

    Button button2 = new Button("Next page"); 
    button2.addClickHandler(new ClickHandler() 
    { 
    public void onClick(ClickEvent event) 
    { 
     History.newItem("second_page"); 
    } 
    }); 
    panel.add(button2); 
    initWidget(panel); 
} 

public static FirstPanel getInstance() 
{ 
    if(instance == null) 
    return instance = new FirstPanel(); 
    else 
    return instance; 
} 

public Widget getWidget() { 
    return panel; 
} 

public void onHistoryChanged(String historyToken) { 
if(historyToken.equalsIgnoreCase("second_page")) 
    ContentPanel.getInstance().setContent(SecondPanel.getInstance()); 
} 

} 

と最後ではなく、少なくとも:))

public class SecondPanel extends Composite implements HistoryListener 
    { 
    private static SecondPanel instance; 
    public SecondPanel() 
    { 
     History.addHistoryListener(this); 
     VerticalPanel verticalPanel = new VerticalPanel(); 
     TextBox firstnameBox = new TextBox(); 
     TextBox lastnameBox = new TextBox(); 
     Button submitButton = new Button("Click"); 

     verticalPanel.add(firstnameBox); 
     verticalPanel.add(lastnameBox); 
     verticalPanel.add(submitButton); 

     submitButton.addClickHandler(new ClickHandler() 
     { 
     public void onClick(ClickEvent event) 
     { 
      History.newItem("first_panel"); 
      alert("You are in second panel"); 
     } 
     }); 
     initWidget(verticalPanel); 
    } 

    public static SecondPanel getInstance() 
{ 
    if(instance == null) 
     return instance = new SecondPanel(); 
    else 
     return instance; 
} 
    public void onHistoryChanged(String historyToken) 
    { 
     if(historyToken.equalsIgnoreCase("first_panel")) 
     ContentPanel.getInstance().setContent(FirstPanel.getInstance()); 
    } 
    } 

問題は、私はFirstPanelのボタンをクリックし、SecandPanelが、その後 "を押すロードされていることですブラウザでは何もしません。onHistoryChangedメソッドでは、param historyTokenは空の文字列であり、スタック(first_page)からの値ではありませんか?

誰かが問題を見つけたり、私が間違っている箇所を教えてくれたら、私は感謝します。

おかげで最初のページロードで

答えて

3

あなたは手でonHistoryChanged(INIT_STATE)を呼んでいます。これは歴史を変えない。これで置き換えます

public FirstPanel() { 

    History.addHistoryListener(this); 
    String token = History.getToken(); 
    if (token.length() == 0) { 
     History.newItem(INIT_STATE); 
    } else { 
     History.fireCurrentHistoryState(); 
    } 

    .. rest of code 

より良い練習しかないから履歴に基づいて、一番上のパネル(エントリーポイントまたはContentPanel)とスイッチパネルに履歴リスナーHistory.addHistoryListener(..)を登録することです。

+0

ありがとうピーター、そうです。それは機能します! – adgfs

+0

トップレベルのパネルにリスナーを1つだけ持つことを意味します。 – adgfs

+0

はい、コードを読みやすくします。 –