4 Replies Latest reply on Aug 2, 2013 3:07 PM by Anton Hughes

    how to inject a list?

    Anton Hughes Master

      Hi all

       

       

       

      We have a page, that calls a service - and this service returns a list<Indicator>.

       

      Then, we have several client Composite components that dependant on this List<Indicator> result.

       

      These components are instantiated at different times.

       

      My thinking was that, perhaps we can @Inject the List<Indicator>, however this does not work.

       

      I also tried using an Event<Indicator> with an Observer, however, I assume, due to the event firing before the components had been instantiated,this did not work.

       

      Currently, we have a tightly coupled dependancy on the List<Indicator> being passed on the page that the components are used.

       

      I'm hoping we can inject this in some way.

       

      What / how do you advise we achieve this?

        • 1. Re: how to inject a list?
          Erik Jan de Wit Novice

          Hi Anton,

           

          Do you mean how to use them in errai-ui? If so you can use an ListWidget or create an Provider to inject the list

           

          Cheers,

               Erik Jan

          • 2. Re: how to inject a list?
            Anton Hughes Master

            Hi Erik

             

            The data in the List<Indicator> is used in the UI, however, the data in the list is displayed differently in different components.

             

            I am already using the ListWidget now to display the items on the page.

             

            However, there are other components that need this data.

             

            For example, we have a component that shows in a modal window, and uses data from the List<Indicator>. We either have to explicity pass this list from the page to the component, or again call the service.

             

            So, I am looking for a way to:

            Call the service once, and retrive the List<Indicator>

            And have this value available for client components without being tightly coupled.

            • 3. Re: how to inject a list?
              Jonathan Fuerth Master

              Have you tried an application-scoped producer? Maybe something like this:

               

              @ApplicationScoped
              public class IndicatorBeanProvider {
              
                @Produces
                private List<Indicator> indicatorList;
              
              
                @AfterInitialization
                private void fetchIndicatorList() {
                  indicatorService.call(...callbacks...).getIndicators();
                }
              }
              

               

              and in the callback from the RPC, assign the response value to indicatorList. Then wherever you @Inject List<Indicator>, the CDI context should inject the same list that the producer field references.

               

              I think this should work for your use case. It would not work if you required a different list of indicators to be injected at different injection points, although you could possibly use CDI qualifiers to solve that problem.

               

              -Jonathan

              • 4. Re: how to inject a list?
                Anton Hughes Master

                Thank you Jonathan!

                 

                That tip signifcantly cleaned up our code!