widget.on is a null reference in plugin code

Comments

3 comments

  • Avatar
    Moti Granovsky (Edited )

    Hi John,

    I assume this code is taken from a widget script (widget extension). When writing a widget script, it is executed in a specific context where a "widget" constant is available containing the widget instance.

    However, in add-ons (plugins) the code is executed in the context of the entire application, regardless of current dashboard/widget or there even being one. In order to affect widgets, one of two things need to be done:

    1. To work with all widgets, in all dashboards

    For this you would need a global event giving you access to the dashboard and widget objects. A simple example of this is:

    prism.on('dashboardloaded' () => { });

    You can read more about the global events here: https://sisense.dev/reference/js/prism/#events

    2. To work only with specific widgets

    While you could use the previous approach and a condition to identify specific widgets, a better approach is to allow dashboards designers to turn your feature on for select widgets.

    The absolute simplest way to do this is to have something like this in your plugin:

    // ensure prism.plugins is defined
    prism.plugins = prism.plugins || {};

    // add your function that does something to a widget
    prism.plugins.myFunction = (widget) => {
      // your code here
    };

    Then, you could apply this to an individual widget by simply adding the following line to that widget's script:

    prism.plugins.myFunction(widget);

    The benefit of this approach is that the logic itself is written once in the plugin's code, and is just used in various widget scripts, making it easy to maintain - versus having copies of the entire code in each widget.

    0
    Comment actions Permalink
  • Avatar
    John Ruf

    Moti,

    I need to customize existing Sisense widgets to hide columns right before the users has access to view.  

    The event below is before the widget renders, so I do not have the ability to inspect the widget.  In the events, I see listing for when the widget is loaded in the editor.  Do you have an event for widgets right before they render to the user?

    prism.on('dashboardloaded' () => { });
    0
    Comment actions Permalink
  • Avatar
    Moti Granovsky

    Hi John, my apologies for not explaining myself properly:

    Because widgets only exist within the dashboard context, to register to widget events you must have a dashboard object available, which is why a global event is used on the dashboard level.

    Within that event's handler, as you have access to the dashboard object, you can register handlers for events on the dashboard level, which include `widgetready`. The two events below are the same, except on the dashboard level the event fires for every widget in the dashboard, while on the widget level it fires for the specific widget.

    dashboard.on('widgetready', ...) 
    widget.on('ready') 

    So in a plugin, you would nest these events, like so:

    prism.on('dashboardloaded', function (ev, args) {
    console.log('dashboard loaded: ' + args.dashboard.oid);
    args.dashboard.on('widgetready', function (ev, args) {
    console.log('widget ready: ' + args.widget.oid);
    });
    });

    Note that a plugin with this code will run this for every dashboard being viewed and every widget within it. If you only need to apply changes to a select few widgets, taking the second approach where it is applied explicitly to specific widgets will be more efficient.

    0
    Comment actions Permalink

Please sign in to leave a comment.