Dashboards & Visualizations

How to include Javascript file from other Javascript file?

VatsalJagani
SplunkTrust
SplunkTrust

How to include Javascript files from another Javascript file from the local appserver/static folder

The below code somehow does not work:

 

require([
    'jquery',
    './myCustomUtils',
    'splunkjs/mvc',
    'underscore',
    'splunkjs/mvc/simplexml/ready!'], 
function($, myCustomUtils, mvc, _){

 

Labels (1)
0 Karma
1 Solution

VatsalJagani
SplunkTrust
SplunkTrust

Answering my own question so I don't have to spend a huge amount of time that I spent today.

 

You need to import that file, not from the local folder but instead like this:

require([
    'jquery',
    '../app/<my_app>/myCustomUtils',
    'splunkjs/mvc',
    'underscore',
    'splunkjs/mvc/simplexml/ready!'], 
function($, myCustomUtils, mvc, _){

 - '../app/<my_app>/myCustomUtils' 

 

I hope this helps others in the future!!! Upvote if it does!!

View solution in original post

0 Karma

VatsalJagani
SplunkTrust
SplunkTrust

Answering my own question so I don't have to spend a huge amount of time that I spent today.

 

You need to import that file, not from the local folder but instead like this:

require([
    'jquery',
    '../app/<my_app>/myCustomUtils',
    'splunkjs/mvc',
    'underscore',
    'splunkjs/mvc/simplexml/ready!'], 
function($, myCustomUtils, mvc, _){

 - '../app/<my_app>/myCustomUtils' 

 

I hope this helps others in the future!!! Upvote if it does!!

0 Karma

VatsalJagani
SplunkTrust
SplunkTrust

Notes:
* You need to use define() in the file that is being imported.
* You need to export the classes and functions from the file that is being imported.

 

Example:
app/my_app/appserver/static/utility.js

define([
    "underscore",
    "jquery"
], function (
    _,
    $
) {
    class MyUtility {
    }

    return {
        'MyUtility': MyUtility
    }
});

 

app/my_app/appserver/static/main.js

require([
    "splunkjs/mvc",
    "underscore",
    "jquery",
    '../app/my_app/utility.js'
], function (
    mvc,
    _,
    $,
    CommonUtility
) {

CommonUtility.MyUtility();

});

 

0 Karma
Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...