Execute JavaScript code from a plugin once after load


(Orlando Del Aguila) #1

Hello!,

I’m looking a way to execute JavaScript code after Discourse has finished loading. I’m using the following approach as I’ve seen it some plugins.

// Inside an initializer
export default {
  name: 'my-initializer',
  initialize(container) {
    withPluginApi('0.8.9', api => {
      api.modifyClass('component:site-header', {
        @on('didInsertElement')
        myInitializer() {
          Ember.run.scheduleOnce('afterRender', () => {
            // my code here
          });
        },
      });
    });
  },
};

Is there a way to do specifically this, without hooking to the site-header component lifecycle? Like a window.onload but for Discourse.

Thanks!


(Kane York) #2

Well, they’re hooking into the site-header because those plugins need to modify the header :slight_smile:

Can you be a little more specific about what this code needs to do?


(Orlando Del Aguila) #3

Render a modal conditionally given some user custom_fields. The work is related to this Force password change after login


(Kane York) #4

In that case, you can probably stick the code in </body> in a theme component. The base classes e.g. Discourse.User.current() exist and are loaded at that point.


(Orlando Del Aguila) #5

In my case I want to do it inside a plugin, like what’s the right way to do it from a plugin. I’ll update the title sorry