cancel
Showing results for 
Search instead for 
Did you mean: 
intapiuser
Community Team Member
Community Team Member
Question: how to invoke functions from within sisenseJS? I would like to understand this invocation process for a function that lives within a dashboard script AND a plugin.
Answer:
Functions in a widget/dashboard script or a plugin aren't (and cannot be) automatically exposed somewhere, because they run in an enclosed scope just like any Javascript function (or any scoped block for that matter, including loops for example):
function myScopedBlock() {
    function myInternalFunc() {
        // do something
    }
}

console.log(myInternalFunc); // undefined
As you can see in the example above, simply defining a function in a scoped block does not automatically allow you to invoke it elsewhere, and you need to explicitly expose it - how that's done depends on the environment you're working in. For example, in a Node.js script using CommonJS modules, you would use the module.exports syntax.
In the case of dashboard or widget scripts, in their context you always have access to an object called either "widget" or "dashboard" respectively, which represents the current instance of the dashboard/widget the script is running in. You can attach custom properties to that object, which can be functions as well, for example in a widget script you could add:
widget.myFunc = function () {
    console.log('this is a custom function in widget ' + widget.oid);
};
Now in your Sisense.js code you can wait for the widget to load, and once it does, you will have access to the function you've attached to it:
Sisense.connect('http://localhost:30845/').then(function(app) { 
    app.dashboards.load('5e9d6748d81796002d373530').then(function(dash) { 
        let widgetInstance = dash.widgets.get('5e9d676ed81796002d373533');
        widgetInstance.container = document.getElementById('widget1');
        widgetInstance.on('ready', function(sender, ev){ 
            // "sender" is your widget object in the event's context
            sender.myFunc();
        });
        dash.refresh();
     });
});
Same can be done for dashboards.
Rate this article:
Version history
Last update:
‎02-14-2024 10:47 AM
Updated by:
Contributors