Using the 'Discourse' javascript class in a hosted forum


(Garrett Boatman) #1

Hi! Developer fairly new to Discourse here.
I have a hosted forum, so I’m trying to hook into the Discourse class via a <script> block in </body> Customization section of the admin.

I see examples in this forum of application views like Discourse.ApplicationView, Discourse.DiscoveryCategorysView, and Discourse.CloakedView.
However, none of these are available in the Discourse class I see.

The Discourse class I see:
23 PM

All I’m trying to do is doSomething() after a new view gets loaded. Like…

Discourse.ApplicationView.reopen({
    didInsertElement: function() {
        this._super();
        doSomething();
    },
    
    pathChanged: function() {
        Ember.run.scheduleOnce('afterRender', this, function() {
            doSomething();
        });
    }.observes('controller.currentPath', 'controller.model')
});

But I get Cannot read property 'reopen' of undefined because Discourse.ApplicationView doesn’t exist.

Any way to access the application view without writing a plugin?


(Simon Cossar) #2

You can probably do what you want with the plugin-api. Take a look at this topic: Using the PluginAPI in Site Customizations and this file: discourse/plugin-api.js.es6 at master · discourse/discourse · GitHub.

Depending on what you’re trying to do, the modifyClass or onPageChange functions could be useful.


(Garrett Boatman) #3

@Simon_Cossar - that’s exactly what I needed, thank you!

For future devs:

<script type="text/discourse-plugin" version="0.1">
  api.onPageChange((url, title) => {
    doSomething();
  });
</script>

^^ in the </head> Customize tab.