你所寻找的功能是基于模板中的一个名为 isGrandParent 的属性来决定的。
当一个分类拥有孙分类时,我们会渲染相应的孙分类标记;否则,则渲染子分类。
最简单的解决方法是在 parent-category-row Ember 组件中覆盖该属性。
你可以使用 modifyClass 插件 API 方法来实现这一点。
Developing Discourse Themes & Theme Components
modifyClass 允许你重新打开 parent-category-row Ember 组件类并添加新方法。
为了确保这些方法能够执行且数据可用,你需要将它们绑定到 didReceiveAttrs 钩子。
为此,你可以使用内置的 @on 装饰器。
准备好这一切后,你可以简单地通过该组件模板渲染的所有分类来覆盖 isGrandParent,并将它们设置为 false。
以下是整合后的完整代码:
<script type="text/discourse-plugin" version="0.8">
const { on } = require("discourse-common/utils/decorators");
api.modifyClass("component:parent-category-row", {
@on("didReceiveAttrs")
removeGrandchildCategories() {
this.category?.set("isGrandParent", false);
}
});
</script>
将此代码添加到你的主题的 header 标签页下的 common 部分。
这将导致模板仅渲染子分类,而忽略孙分类。
你可以使用相同的模式来修改任何组件中的任何属性。