The 2nd location is via pluginOutlet, below-site-header.
I believe I can’t do the same as the above, using api.decorateWidget, because this is pluginOutlet not a widget(?).
My questions are:
How to manually insert ajax content to the pluginOutlet?
I would like to know also how to compile a virtual-dom on the fly after my ajax call? eg. the variable above, headerNav, I’d like to get a compiled HTML markup of it. Not sure which lib / function to use.
If possible, how to also rerender a pluginOutlet? similar to appEvents.trigger("site-header:force-refresh");
Keep your widget decorator as-is, and create a separate Ember component that does what you want in the plugin outlet. We’ve been phasing out widgets in favor of Ember components (which is what most of Discourse is built on).
This process would look like:
Create a component JS and HBS (template) file in your theme’s javascripts/discourse/components directory.
component-name.js
component-name.hbs
Build your Ember component… unfortunately this step is essentially “learn Ember” (Ember Guides) … but I think this might give you a rough idea to start:
In component-name.js:
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
const endpoint = settings.site_navigation_links_endpoint;
export default class ComponentName extends Component {
@tracked navLinks = null;
@action
async fetchNavLinks() {
try {
const response = await fetch(endpoint);
const data = await response.json(); // assuming this is json
this.navLinks = data;
} catch (error) {
console.error("Failed:", error);
}
}
}
This will call the fetchNavLinks action whenever the component is inserted (in this case, when you visit the Discourse site and the app renders). Whenever navLinks is updated, the content of the component will update because it’s a tracked property.
If you want to update the links more often than on component render you’ll need to add some more logic to the JS here… checking if the current route (page) meets certain conditions for example.
This component would be added to a plugin outlet by adding it to the outlet in javascripts/discourse/connectors/below-site-header/my-component-connector.hbs:
I went for the second approach, using component, since widgets are gradually phasing out. Aside from the little typo in function call, in the .hbs file it should be {{did-insert this. fetchNavLinks}}, everything works!
Great to know that we have did-insert, that’s a big relief! I have now finished the task.