2010-05-05 8 views
2

J'ai créé un widget en utilisant GWT uiBinder. Cela fonctionne bien, jusqu'au moment où je veux l'utiliser une deuxième fois. Après avoir appelé le constructeur une deuxième fois, il renvoie uniquement la description brute de XML et les instructions du constructeur (rootElement.add(new HTML("panel1"), leftId);) ne fonctionnent tout simplement pas. Il ne génère aucune erreur ou avertissement.GWT: le widget basé sur uiBinder ne peut pas être instancé une deuxième fois

S'il vous plaît aider

classe Java:

public class DashboardLayout extends Composite { 

final String leftId = "boxLeft"; 
final String rightId = "boxRight"; 

interface DashboardLayoutUiBinder extends UiBinder<HTMLPanel, DashboardLayout> { 
} 

private static DashboardLayoutUiBinder ourUiBinder = GWT.create(DashboardLayoutUiBinder.class); 

@UiField 
HTMLPanel htmlPanel; 

public DashboardLayout() { 
    HTMLPanel rootElement = ourUiBinder.createAndBindUi(this); 
    this.initWidget(rootElement); 

    rootElement.add(new HTML("panel1"), leftId); 
    rootElement.add(new HTML("panel2"), rightId); 

} 
    } 

XML descriprion:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder' 
      xmlns:g='urn:import:com.google.gwt.user.client.ui' 
      > 
    <g:HTMLPanel ui:field="htmlPanel"> 
     <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
      <tr> 
       <td width="40%" id="boxLeft" class="boxContextLeft"> 

       </td> 

       <td width="60%" id="boxRight" class="boxContextRight"> 

       </td> 
      </tr> 
     </table> 
    </g:HTMLPanel> 
</ui:UiBinder> 
+0

Je ne peux pas reproduire le comportement que vous décrivez. Votre widget UiBinder semble être correct. Comment ajoutez-vous le widget à un widget parent (RootPanel ou un enfant)? –

+1

Oh, je sais ce qui pourrait être votre problème. Vos identifiants boxLeft et boxRight ne sont pas uniques si vous ajoutez le widget au DOM une seconde fois. –

Répondre

7

Ne pas utiliser id="myid" dans les widgets, car ils seront global (qui vous bousiller) au lieu de portée par instanciation du widget; utilisez ui:field="myid", puis créez une variable UiField correspondante dans la classe java. Cela permettra au compilateur gwt d'obscurcir l'identifiant de sorte que vous n'obtiendrez pas de collisions entre les instanciations du même widget.

DashboardLayout.java

public class DashboardLayout extends Composite { 

    interface DashboardLayoutUiBinder extends 
      UiBinder<HTMLPanel, DashboardLayout> { 
    } 

    private static DashboardLayoutUiBinder ourUiBinder = GWT 
      .create(DashboardLayoutUiBinder.class); 

    @UiField 
    HTMLPanel htmlPanel; 

    @UiField 
    HTML panel1; 

    @UiField 
    HTML panel2; 

    public DashboardLayout() { 
     HTMLPanel rootElement = ourUiBinder.createAndBindUi(this); 
     this.initWidget(rootElement); 

     // do stuff with panel1 
     panel1.setHTML("<blink>blink</blink>"); 

     // do stuff with panel2 
     panel2.setHTML("<marquee>marquee</marquee>"); 
    } 
} 

DashboardLayout.ui.xml

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder' 
    xmlns:g='urn:import:com.google.gwt.user.client.ui'> 
    <g:HTMLPanel ui:field="htmlPanel"> 
     <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
      <tr> 
       <td width="40%" class="boxContextLeft"> 
        <g:HTML ui:field="panel1"></g:HTML> 
       </td> 

       <td width="60%" class="boxContextRight"> 
        <g:HTML ui:field="panel2"></g:HTML> 
       </td> 
      </tr> 
     </table> 
    </g:HTMLPanel> 
</ui:UiBinder>