Add id to field with ui:field declaration

前端 未结 3 1263
面向向阳花
面向向阳花 2021-02-05 18:00

I\'m trying to declare these elements in my UiBinder XML:




        
3条回答
  •  温柔的废话
    2021-02-05 18:51

    UiBinder uses the ID to implement its ui:field magic, so no you can't set it from the XML.

    The way to do it is to have a Java constant with the ID and use it from both sides:

    @UiField(provided = true)
    final String lastNameId = Document.get().createUniqueId();
    
    @UiField InputElement lastNameField;
    
    …
    
    lastNameField.setId(LAST_NAME_ID);
    

    and in the XML:

    
    
    

    Note that I haven't tested the above code with type="java.lang.String", I've always used a class containing various identifiers instead (or rather, an interface with a generator)

    Alternatives are:

    • if you can, use the alternate syntax for :

      
      
    • read the for="" value from Java to use it in setId(), that way at least you remove duplication, but you'll still have the issue that your IDs possibly won't be unique (as soon as you use your UiBinder-widget more than once)

      
      
      
      @UiField LabelElement lastNameLabel;
      @UiField InputElement lastNameField;
      
      …
      
      lastNameField.setIf(lastNameLabel.getHtmlFor());
      

提交回复
热议问题