[kianba 5.1.1] How to injectVars in node controller?

kibana version: 5.1.1
question:

  1. how to injectVars in node controller?
    or
    is there some way that I can pass value from node-backend route to chrome ui
    or
    any way that i can injectVars from one app to all apps?

is there anyone who can give me some help? thanks very much

Hi whoami,

I'm not sure what you mean about 'injectVars in node controller'. There are a lot of ways to have shared variables in node, such as using a file for constants, or using config settings.

As for the second part, "injectVars from one app to all apps", you can use injectDefaultVars for that. That gives you the ability to share variables created in your plugin's index.js throughout Angular modules of any plugin.

Quick example:

export default function (kibana) {
  return new kibana.Plugin({
    id: 'myplugin',
    publicDir: resolve(__dirname, 'public'),
    uiExports: {
      app: {
        title: 'My Plugin',
        injectVars(server) {
          const config = server.config();
          // visible to `myplugin`
          return {
            something: config.get('myplugin.something')
          };
        },
      },
      injectDefaultVars(server) {
        const config = server.config();
        // visible to any plugin
        return {
          somethingelse: config.get('myplugin.somethingelse')
        };
      }
    }
...

my case is that the value I wanna injected as default come from other plugin abc

injectDefaultVars(server) {
const v = server.plugins.abc.value;
// visible to any plugin
return {
somethingelse: v
};
}

You can have the other plugin expose an object on the server object, and access that object from other plugins. You probably don't want to do this from a controller. A controller's handler should only be responsible for handling a single HTTP request. Instead handle it in the init of the plugin, so the objects get set up on Kibana startup:

In pluginone/index.js

    ...
    init: function (server, options) {
      const helpers = {
        sayHello() {
          console.log('hello');
        }
      };
      server.expose('helpers', helpers);
      this.status.green('ready to go');
    }
    ...

In plugintwo/index.js

export default function (kibana) {
  return new kibana.Plugin({
    require: ['pluginone'],
    ...
    init: function (server, options) {
      const { pluginone } = server.plugins;
      pluginone.helpers.sayHello();
      this.status.green('also ready to go');
    }
  });
};

Note there is no official support or API for 3rd party plugin. Until there is, these things could break from one release to another.

yeah, i know that. the question is that I have to expose it in node controller. the var to be exposed is in request.session

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.