Dashboards & Visualizations

How to import the token value in the JavaScript in Simple XML?

kedjjang
Path Finder

alt text
Create a wish come test.js code to get the value of $ time_ok1.earliest $.

jeffland
SplunkTrust
SplunkTrust

In test.js, you need to require the right components, and then you can simply grab the tokens from the token model. Something like this:

 require([
     'splunkjs/mvc/simplexml/ready!',
     "splunkjs/mvc"
 ],
 function(
     mvc
     ) {
         // Get the default model
         var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
         // Get some token from there
         var token_kang = defaultTokenModel.get("kang");
         ...

See here for the docs. Be aware though that the object "token_kang" at the moment contains two fields, and to get to the earliest in js, you need to do token_kang.earliest.

joao_amorim
Communicator

Why the answers are not being posted here

0 Karma

jeffland
SplunkTrust
SplunkTrust

Your comment might not be published immediately because it is in moderation.
Two things: have you surrounded your js in a splunkjs/ready!-require statement? That should ensure everything is loaded before your code runs. Also, does your input have a default setting?

0 Karma

joao_amorim
Communicator

Ok yes it has a default setting, ah i didn't has the splunkjs/ready! in the require statement.
Now it looks like it's working. But now i want to add behavior to the time input like on change

0 Karma

jeffland
SplunkTrust
SplunkTrust

You can do that by listening to the token model change:

var tokens = mvc.Components.get("default");
tokens.on("change:token_name", function(model, value, options) {
    // do stuff
});
0 Karma

joao_amorim
Communicator

Ok ty i will try that in the other tokens i usually do this:
JS:
var year = $("#input1");
year.on("change", function(newValue) {
refreshHOT();
});

XML:

This works for the other tokens but since time inputs are not dropdowns, that's why this doesn't work for them.
Or it's more reliable to change them all to your example of:

tokens.on("change:token_name", function(model, value, options) {
// do stuff
});

0 Karma

jeffland
SplunkTrust
SplunkTrust

If that was a question, no, I doubt one way is more reliable than the other. See which one works for you, and go with it... or use both.

0 Karma

joao_amorim
Communicator

I changed them to you suggestion and they work, but again the time input token doesn't work.
But probably to time input i need to do:
tokens.on("change:time_range.earliest", ...)
Not only:
tokens.on("change:time_range", ...)

0 Karma

jeffland
SplunkTrust
SplunkTrust

Yes, that's probably it.

0 Karma

joao_amorim
Communicator

Thanks unfortunately the behavior after using your method was not the expected but i give a try to the method explained by kedjjang and it works perfect.
Just if you want to know:
XML:

<input id="time_range" type="time" token="time_range" searchWhenChanged="true">

JS:

var time_range = mvc.Components.getInstance("time_range");
time_range.on("change", function(newValue) {
    // do stuff
});
0 Karma

jeffland
SplunkTrust
SplunkTrust

Thanks for posting a working solution. Please enter xml code by leavin one blank line and indenting all lines of code by four spaces, otherwise the board will mess your code up.

0 Karma

joao_amorim
Communicator

The input tag makes code disappear xP

0 Karma

joao_amorim
Communicator

When i open the dashboard the only tokens that don't work are these related to time inputs because the others work fine. As I have the tokens set are working but just after i change the time input to other date. The first time the dashboard loads, the get of the earliest and latest gives undefined

0 Karma

kedjjang
Path Finder

It appears to be undefined.

splunkjs.mvc.Components.getInstance ("default");

Will that be called justice in simplexml default?

0 Karma

jeffland
SplunkTrust
SplunkTrust

What exactly is undefined?

0 Karma

kedjjang
Path Finder
require(["splunkjs/ready!"], function(mvc) {
var deps = [
        "jquery",
        "splunkjs/ready!",      
        "splunkjs/mvc/searchmanager",
    ];
    require(deps, function(mvc) {               

    var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
    var token_def = defaultTokenModel.get("kang");

    alert(token_def);  << undefined
    console.log(token_def);
});
});
0 Karma

jeffland
SplunkTrust
SplunkTrust

Oh, my bad. I mistook your id and token name. It has to be

var token_def = defaultTokenModel.get("time_ok1");

Sorry!

0 Karma

kedjjang
Path Finder

You're welcome...

"time_ok1" Even if I value the results.
I think that haneunge "default" settings on the source code ... I do not know anything.

0 Karma

jeffland
SplunkTrust
SplunkTrust

I'm sorry, I don't quite get what you're saying - is something still not working?

0 Karma

joao_amorim
Communicator

Hi this var token_def = defaultTokenModel.get("time_ok1");
Doesn't work for me. Any idea?

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to November Tech Talks, Office Hours, and Webinars!

&#x1f342; Fall into November with a fresh lineup of Community Office Hours, Tech Talks, and Webinars we’ve ...

Transform your security operations with Splunk Enterprise Security

Hi Splunk Community, Splunk Platform has set a great foundation for your security operations. With the ...

Splunk Admins and App Developers | Earn a $35 gift card!

Splunk, in collaboration with ESG (Enterprise Strategy Group) by TechTarget, is excited to announce a ...