Hi,
I have a Chart component to draw a Google chart. I now pass the
calculated data as a parameter to the component. But this is not
optimal, as sometimes these calculations can take some time. That's why
I wanted to change the component so it does the calculation in a ajax
request (using jQuery) after the component rendered and show a loading
image in the meanwhile.
It works, but I'm struggling with finding the best way of passing the
needed values for the calculations to this ajax request (Tapestry event).
So what I have now works, but is not optimal.
Code snippets of what I have now:
*SomePage.tml*
<t:chart ... t:params="chartParams" />
*SomePage.class*
@Property
private String someFormValue;
...
/* these values will be used in the jquery post request to the getData
event */
public JSONObject getChartParams() {
JSONObject params = new JSONObject();
...
params.put("someFormValue",someFormValue);
...
return params;
}
JSONArray onGetDataFromSomeChart() {
JSONArray data = new JSONArray();
/* property someFormValue will be set and can be used */
...
return data;
}
...
*Chart.class**:*
@Parameter
private JSONObject params;
...
void afterRender() {
JSONObject spec = new JSONObject();
...
spec.put("data", componentResources.createEventLink("getData").toURI());
spec.put("params", params);
...
javaScriptSupport.addInitializerCall("drawChart", spec);
}
*chart.js*
(function($){
T5.extendInitializers(function(){
function init(spec){
...
$.post(spec.data, spec.params, function(chartData){
...
}
...
}
return {
drawChart: init
}
});
})(jQuery);
While writing this mail I thought of following tweak to my solution:
Instead of the "params" parameter I could add a Block parameter
containing hidden fields for all the needed values, which will be
rendered with the chart. Then for the ajax request I can get all the
needed parameters and values from these hidden fields to use in the post
request.
This already seems a better solution to me, but I'm still unsure about
the best practice for this kind of problem.
So what do you think of this solution. Is this the way to go?
Nathan