Invoke custom dashboard function from sisense.js

Comments

4 comments

  • Avatar
    Moti Granovsky

    Hi David!

    This is a great question, but the answer differs slightly between plugins and widget/dashboard scripts.

    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.

    If your code is defined in a plugin, then the difference would be you would first need to subscribe to a global event using prism.on to get a widget or dashboard object to attach to.

    Hope this answers your question!

    0
    Comment actions Permalink
  • Avatar
    David Phenicie

    Thanks Moti Granovsky ; I tried your approach and can see how this implementation would work, unfortunately I'm running into a snag. 

    When I add to my dashboard script, the new variable is available to the dashboard right away within sisnese dashboard, but if I am accessing the dashboard instance from sisense.js , the new property or function is not available. To make the property available, I must export the dashboard, manually add the property and then re-import the dash file, and then it is visibile from sisense.js implementation. 

    What i have found in doing this process, dashboard properties that are not available to sisense.js are only shown in the 
    "script" string. When the new additions are available, they have their own property within the dashboard instance..

    To recap, when saving the script on the dashboard with an addition of "dashboard.findme = "Find me;" this is found within the script string, but not as a property.

    Any input on how to make this functional without manually editing the dash files?

    0
    Comment actions Permalink
  • Avatar
    Moti Granovsky

    Hi David,

    I did not encounter that issue - the approach I suggested above worked without having to export/import the dashboard.

    There are a few things I can suggest to check why this is occuring:

    1. Make sure you save the widget script and the widget itself after adding to it (and by the way, I'd probably go for plugins instead of widget script in this instance, easier to maintain!)
    2. Make sure you reload your Sisense.js host page without caching (or clear cache and reload)
    3. Make sure you subscribe to the dashboard "ready" event and only try to access your custom property there. if you try accessing this property earlier, it will not exist because the script hasn't run yet (again less of an issue with plugins)
    4. Make sure you are opening the dashboard in the Sisense UI (to append to the script) and in Sisense.js using the same user identity - if you are the dashboard owner when making this change, but are trying to view it as another use to whom the dashboard is shared, you must republish the dashboard for the changes to apply

    Note that most of these comments apply only to widget scripts, as they are inherently a property of the dashboard/widget. A plugin is external, and does not depend on republishing or which user is opening it, so that might be easier.

    0
    Comment actions Permalink
  • Avatar
    David Phenicie (Edited )

    Moti Granovsky Appreciate your help with this. 

     

    I have my script working within sisense UI, but I have found when I am accessing the dashboard instance from sisense.js, the dashboard script is not called. I verified this by adding a console.log in dashboard script. It does not display on my sisense.js implementation, but I do see if inside the sisense ui

     

    As it seems the plugin route, is the correct implementation of my needs, I attempted this. I created a very basic plugin, with the end going being to share a function, that receives a widget as a prop and alters the widget and refresh. As i mentioned before this code is working within sisense UI.

    The sisense plugin I added is like below;

    prism.on("dashboardloaded", dashboard.swapDim = swapDim);

    const swapDim = (widget) => {
    .... Code to alter widget jaql ...
    widget.refresh()
    }

    I am having trouble understanding how to access the swapDim function from my dashboard instance though. Can you please shed some light on that?

     

    Thanks again

    0
    Comment actions Permalink

Please sign in to leave a comment.