2

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?

1 Answer 1

1

Brian, I think you can address this problem by adding the following steps to your approach:

  1. Before you define your namespace, create a dummy variable: var noData = new Object();
  2. When defining the component, specify data="noData". This is simply to avoid JavaScript runtime errors. As you would expect, no data will be bound to the chart.
  3. At the end of the page, add another script element and simply call your vfStudy.refreshRemoteChart() function

I tried this out in my DE org and was able to get it to work (I think) the way you want. You do need a few extra lines of code, but you'll end up with a clean global namespace. Please take a look at this gist for reference: https://gist.github.com/martyychang/2162c3f467927f7b3601

7
  • Cheers for this! Will try it out on Monday when I'm back at the office. :-) Nov 22, 2014 at 1:37
  • Tried it, but it does not work. Problem may be related to salesforce.stackexchange.com/questions/56833/… ... but even going with code direct from the tutorial instead of my own, this still doesn't work. Nov 24, 2014 at 9:10
  • Thanks for the update, @BrianKessler. I wonder why the code works for me but not for you. What is the behavior you're seeing? Here's a walkthrough of what it looks like in my DE org: youtube.com/watch?v=VAZdCgMWbdk
    – Marty C.
    Nov 24, 2014 at 13:54
  • There isn't much behavior to see. The page renders, minus the chart. The word "loading..." displays, as if I changed the year. But then nothing happens; neither a chart is rendered nor the "loading..." text disappears. Nov 24, 2014 at 14:24
  • 1
    Ah, yes. The namespace is a trick (and I haven't figured out how best to address that when posting gists) that I discovered as I was trying to figure out why my code wasn't working after I copied and pasted the tutorial code. Luckily the apex:chart documentation explained its use.
    – Marty C.
    Nov 24, 2014 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .