Dashboards & Visualizations

JavaScript: how to create a class to be used in HTML dashboards?

arkadyz1
Builder

I'm trying to create a JavaScript class (a simple one - no inheritance or anything) which would populate an HTML document with some UI and would be fed some events from Splunk searches (one by one) for some custom representation.

The body of my JavaScript looks like this:

var MyClass = function(args) {
  ...
  this.container = $(args.element);
  this.container.append(...);
  ...
}

MyClass.prototype.CONSTANT = ...;

MyClass.prototype.function1 = function(...) {
  ...
}

MyClass.prototype.function2 = function(...) {
  ...
}

As you can see, it uses jQuery - something I will have to require. I was somewhat successful wrapping everything in require(["jquery"], function($) { <MyClass body> }, but if I simply include my javascript file in the HTML like this:

<script src="{{SPLUNKWEB_URL_PREFIX}}/static/app/myAppName/myclass_test.js" type="text/javascript"></script>

MyClass is not visible to the inline JavaScript. I guess I'll need to remove that &lt;script&gt; line from my HTML and require the file from within the JavaScript (in this case I won't have to require jquery into my file - it's already there, along with underscore, backbone, mvc etc.). How should I properly wrap my code to be used with require? And how do I specify the file location?

My objective is to achieve something like this:

var MyClass = require("/somepath/myclass_test.js");

var myClassView = new MyClass({ ..., element:"#myDiv", ... });

...
myClassView.addEvent(<event from a Splunk search>);

where myDiv is an id of a div I put inside HTML (similar to the meaning of el argument to various Splunk views, such as TableView). I know how to attach events to the searches (real-time in my case) returning new data, so the only part missing is how to properly include it - to figure out the path in require and the proper wrapping of my class' code.

I wonder if I also have to add anything to require.config call which Splunks puts there inside HTML dashboards.

0 Karma
1 Solution

alacercogitatus
SplunkTrust
SplunkTrust

Ok, so this should work well for you. In your myclass_test.js, inside that function, make sure you return the object. So it should look something like this:

   define(function(require, exports, module) {
    var _ = require('underscore'),
        $ = require('jquery');
    var myClass = { object };
    return myClass;
 });

Then, in your HTML dashboard, find the require.config section.

var AppBase = "{{SPLUNKWEB_URL_PREFIX}}/static/app/myAppName";
require.config({
    baseUrl: "{{SPLUNKWEB_URL_PREFIX}}/static/js",
    paths : {
              "myClass":  AppBase + "/myclass_test"
            },
    waitSeconds: 0 
  });

Now, after that section there should be a section of require statements. To the array ( [] ), add, "myClass" at the end of it, and then in the function section, add myClass. Then in side that Javascript block of the function, you can reference myClass.

require( [ 
      ...
      "myClass"
       ], 
      function(
      ...
      myClass) {
             //// Do ALL THE THINGS
 });

Come find me on #splunk on Efnet IRC to discuss more!

View solution in original post

alacercogitatus
SplunkTrust
SplunkTrust

Ok, so this should work well for you. In your myclass_test.js, inside that function, make sure you return the object. So it should look something like this:

   define(function(require, exports, module) {
    var _ = require('underscore'),
        $ = require('jquery');
    var myClass = { object };
    return myClass;
 });

Then, in your HTML dashboard, find the require.config section.

var AppBase = "{{SPLUNKWEB_URL_PREFIX}}/static/app/myAppName";
require.config({
    baseUrl: "{{SPLUNKWEB_URL_PREFIX}}/static/js",
    paths : {
              "myClass":  AppBase + "/myclass_test"
            },
    waitSeconds: 0 
  });

Now, after that section there should be a section of require statements. To the array ( [] ), add, "myClass" at the end of it, and then in the function section, add myClass. Then in side that Javascript block of the function, you can reference myClass.

require( [ 
      ...
      "myClass"
       ], 
      function(
      ...
      myClass) {
             //// Do ALL THE THINGS
 });

Come find me on #splunk on Efnet IRC to discuss more!

arkadyz1
Builder

OK, after some chat on IRC, I got it working. Thanks!

Just for the future reference: in addition to the constructor defined in myClass (var myClass = function() { ... }), you can have a big myClass.prototype = { var ...; function ...(...) { ... }; or lots of myClass.prototype.member = ...; statements. Just make sure to return the constructor in the end!

0 Karma
Get Updates on the Splunk Community!

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...