The discussion revolves around two main topics: using the h helper from virtual-dom in a custom theme’s <head> section and overriding a computed method in the composer.
Using the h helper
rbrlortie tried to use the h helper to wrap text with a tag but encountered a ReferenceError. david provided a solution by adding const h = require("virtual-dom").h; at the top of the JavaScript code.
Overriding a computed method
rbrlortie attempted to override the categoryValidation() method in composer.js.es6, which is a computed method. However, they encountered issues accessing the @computed decorator from the <head> section.
simon provided an example of modifying a computed property using the Ember.computed function, while david showed how to use the decorator syntax by requiring the “ember-addons/ember-computed-decorators” module.
The final solution, provided by rbrlortie, involves using the require function to access the computed decorator and overriding the categoryValidation() method to throw a custom error message when the user only selects one level of category.
I’m attempting to override the main header-dropdown html tags via my custom theme.
My proof on concept was this:
<script type="text/discourse-plugin" version="0.8.23">
api.reopenWidget("header-dropdown", {
html(attrs) {
if (attrs.icon == 'bars') {
return 'TEXT TO RETURN INSTEAD OF HAMBURGER';
} else {
return this._super(attrs);
}
}
});
</script>
In this example, ‘TEXT TO RETURN INSTEAD OF HAMBURGER’ displays correctly.
Now, I want to wrap this text with a tag.
The solution seems to use the h helper like so:
return h('b', "TEXT TO RETURN INSTEAD OF HAMBURGER");
However, this returns ReferenceError: h is not defined.
Attempting to add import { h } from "virtual-dom"; doesn’t work, which makes sense because I’m in the custom theme head override. (/admin/customize/themes/7/common/head_tag/edit)
What would be the correct way to do this without creating a plugin?
My issue is that from the <head>, I don’t have acces to @computed. Is there a way to require it?
I did manage to override the controller:composer save action. However, from there I don’t have access to InputValidation to throw a custom error (I wouldn’t know how to target the correct input from there either).
<script type="text/discourse-plugin" version="0.8.23">
api.modifyClass('controller:composer', {
@computed("model.categoryId", "lastValidatedAt") //ReferenceError: computed is not defined
categoryValidation() {
console.log('categoryValidation');
this._super();
}
});
</script>
I’m not sure if this is the only way to do it, but I’ve modified computed properties in customizations like this (the example is using the availableSorts computed property):
Notice that these lines are just a re-arranging of the import lines that you see in Discourse’s codebase. So anything you can import, you should be able to require in a theme.