Fragment with provided data help

Hi,

  1. supposing of having a view with multiple instance data container of the same type
<instance id="tipoA_1" property="tipoa1"></instance>
<instance id="tipoA_2" property="tipoa2"></instance>

where tipoa1 and tipoa2 are properties of type TipoA

  1. supposing of creating a fragment for TipoA with provided data
<instance id="tipoA_1"
                  class="TipoA"
                  provided="true">

How can I reuse the fragment for instance tipoA_1 and tipoA_2?

Thanks for helping!

Hi @m.russo

Reusable fragments for one type of entity are available for components displaying a list of entities (see docs).

In your case, you need to customize the implementation of the fragment. Perhaps the way described below will be helpful:

  1. Save an instance container for one of the properties as a fragment property:
    Add a fragment property in the view descriptor:

           <fragment id="tipoA1Fragment" class="com.company.sample.view.typoafragment.TypoAFragment">
                <properties>
                    <property name="sourceEntityDc" value="tipoA_1" type="CONTAINER_REF"/>
                </properties>
            </fragment>
            <fragment id="tipoA2Fragment" class="com.company.sample.view.typoafragment.TypoAFragment">
                <properties>
                    <property name="sourceEntityDc" value="tipoA_2" type="CONTAINER_REF"/>
                </properties>
            </fragment>
    

    Add a field and setter to your fragment controller class:

      private InstanceContainer<TipoA> sourceEntityDc;
     
      public void setSourceEntityDc(InstanceContainer<TipoA> sourceEntityDc) {
        this.sourceEntityDc = sourceEntityDc;
      }
    
    
  2. Add an instance container to your fragment descriptor:

     <data>
         <instance id="entityDc" class="com.company.sample.entity.TipoA"/>
     </data>
    
  3. Set the item in the container from the previous step using the instance container from the view:

    @Subscribe(target = Target.HOST_CONTROLLER)
    public void onHostBeforeShow(final View.BeforeShowEvent event) {
        entityDc.setItem(sourceEntityDc.getItem());
     }
    

Regards,
Maria.

Thanks Maria,

i was able to deal with my problem with a solution very close to your proposal, and i think it would be better

public void setSourceEntityDc(InstanceContainer<TipoA> sourceEntityDc) {
    sourceEntityDc.addItemChangeListener(e -> {
            entityDc.setItem(e.getItem());
        });
  }