Currently looking at Refreshing Chart Data Using JavaScript Remoting
Reading about JavaScript best practices, I came across the idea of keeping functions out of the global namespace, so I tried to refactor the Javascript to look like this:
var vfStudy = vfStudy || {};
vfStudy.refreshRemoteChart = function()
{
$("#statusDisplay").text("Loading...");
retrieveChartData
(
function(statusElement)
{
return function(data)
{
RemotingPieChart.reload(data);
$("#statusDisplay").text("");
};
}
(statusElement)
);
};
vfStudy.retrieveChartData = function (callback)
{
var year = $("#theYear").val();
var retrieveCallback = function(result, event)
{
if(event.status && result && (result.constructor === Array))
{
callback(result);
RemotingPieChart.show();
}
else
{
$("#remoteResponseErrors").text(event.message + ((event.type === 'exception') ? ('<br/>' + event.where) : ''));
}
};
Visualforce.remoting.Manager.invokeAction ( 'Sledgehammer.VF_PieChartVFCtrl.getRemotePieData', year, retrieveCallback , { escape: true } );
};
Moving the refreshRemoteChart
into the vfStudy namespace worked as expect, but even updating data="retrieveChartData"
to data="vfStudy.retrieveChartData"
, the VisualForce Chart would not render.
Is it possible for me to move this function out of the global namespace? If so, how?