Call a UI Script from a UI Page's Client Script

Brian Bush
Giga Guru

I have a custom HighChart template that I re-use for several graphs. I make the graphs available to my homepage through a UI Page referenced by a System UI Widget.

This is an abridged version of the code for the HighChart:

Highcharts.setOptions({

  lang: {

  thousandsSep: ','

  }

});

var ga = new GlideAjax('MyChartUtil');

ga.addParam('sysparm_name', 'getData');

ga.addParam('sysparm_metric', 'CountsDaily');

ga.getXMLWait();

ga.getXML(parseResponse);

function parseResponse(response) {

  var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][new Date().getDay()];

 

  var data = response.responseXML.documentElement.getAttribute("answer").evalJSON();

  var cX = new Date().getHours() + 1;

  var cY = data['span'][cX][1];

  data['span'][cX][1] = null;

  var chart = new Highcharts.Chart({

      chart: {

          renderTo: data['renderto'],

          type: 'spline'

      },

      xAxis: {

          tickInterval: 2

      },

      yAxis: {

          title: false,

          gridLineColor: 'rgba(0,0,0,0.1)',

          gridZIndex: -1,

          min: 0

      },

      series: [{

          type: 'areasplinerange',

          name: 'Typical ' + weekday + ' Range',

          data: data['range'],

          lineWidth: 0,

          color: '#2f7ed8',

          fillOpacity: 0.25,

          enableMouseTracking: false,

          marker: {

              enabled: false

          }

      }]

  });

}

The UI Page is really just a stub. I use it for several graphs. I would like to create a Javascript Object that I can call on demand that would hold all of this, rather than creating copies on every relevant UI Page. This is really simple on the server-side with a Script Include. It seems like a UI Script is what I want on the client-side, but I don't see how to instantiate it on the UI Page.

Has anyone attempted this before? Ideas?

3 REPLIES 3

Jon Barnes
Kilo Sage

Yes, UI Script is the way to go. I would recommend creating your object in a UI script, but do not make it global. You can include that UI script in your UI page with this jelly tag:



<g:requires name="[ui script name].jsdbx" />



Then, you can call your script from there.



Does that answer your question


amaiyp
Kilo Explorer

Hi Jon,

 

I tried the same and its not calling.

find_real_file.png

UI Script:

find_real_file.png

use global before calling ui script.

 

like global.yourUI_script_name.