0

I think my problem is related to BalusC's answer here: Evaluation of EL during view build time

I am trying to display a series of Primefaces BarCharts using a JSF2 composite component, passing in a custom Chart object as an attribute. The Chart object contains the charts name, title and a call to a DAO object to retrieve the data model. Here's my composite component.

<composite:interface>
    <composite:attribute name="chart" />
</composite:interface>
<composite:implementation>
    <p:barChart id="#{cc.attrs.chart.name}" title="#{cc.attrs.chart.title}" value="#{cc.attrs.chart.model}" 
            style="width:300px" legendPosition="ne" xaxisAngle="45"/>
</composite:implementation>    

As Primefaces renders the barchart object, it makes three calls to getValue() for the barchart object, and as explained in the link above, only the EL expression "#{cc.attrs.chart.model}" has been stored. This results in a new model evaluation every time getValue is called internally by Primefaces and so three round trips to the database.

Is there some way to evaluate cc.attrs.chart.model once and use it as the value attribute of the chart?

I think I could use a chart UI component and binding but I wanted as many of the chart properties to be defined in my view as possible so this feels wrong?

Community
  • 1
  • 1
Gordon Little
  • 141
  • 1
  • 2
  • 16

1 Answers1

0

Why don't you use lazy loading?

like:

public Object getValue() {
  if(this.value == null) {
    // do value loading here   
  }
  return this.value;
}
ppawel
  • 1,046
  • 8
  • 17
anm
  • 545
  • 3
  • 17
  • I do, but the chart object evaluated by #{cc.attrs.chart... is a new object every time and so the model member variable is always null again. Note that the getValue() method is on PrimeFaces' BarChart component. It is my Chart.getModel() method which implements lazy loading. I almost feel that Primefaces should be evaluating getValue() once and storing the retrieved model rather than constantly reevaluating it (i.e. they should be lazy loading) but I'm sure there are reasons for it. – Gordon Little Nov 05 '13 at 09:26
  • Is it not possible to use a conversation scoped bean for that? Use a "BarChartHandler" or something like this where you get the model from. Then you can load it only once... – anm Nov 05 '13 at 10:04
  • Sorry, should have added I'm on Tomcat 6 so don't think I have access to ConversationScope? I have considered using a bean but my component is called multiple times from a c:forEach loop with a different chart object for each composite component. The number of charts is dynamic and defined in the database. (I'm making a dashboard essentially). In this case I couldn't define my beans in faces-config.xml or using annotations. I'd need to declare them programmatically? I'm not averse to this but am looking for best advice on where the balance between view and model is. Hope that makes sense. – Gordon Little Nov 05 '13 at 10:35
  • Sorry, I've never used PrimeFaces Charts until now. And I don't know Tomcat very well, we are using IBM Websphere... – anm Nov 06 '13 at 11:24