# 在编辑器中更改类别时触发函数

**URL:** https://meta.discourse.org/t/triggering-a-function-whenever-the-category-is-changed-within-the-composer/388497
**Category:** Development
**Created:** [2025年十一月14日 15:49 UTC](https://meta.discourse.org/t/triggering-a-function-whenever-the-category-is-changed-within-the-composer/388497 "2025-11-14T15:49:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![frye\_bradley](https://avatars.discourse-cdn.com/v4/letter/f/848f3c/32.png) [@frye\_bradley](https://meta.discourse.org/u/frye_bradley)
#### Post date: [2025年十一月14日 15:49 UTC](https://meta.discourse.org/t/triggering-a-function-whenever-the-category-is-changed-within-the-composer/388497/1 "2025-11-14T15:49:12Z")

</div>

我可能遗漏了一些显而易见的东西，如果是这样，我提前道歉。

我的插件为每个类别都有一个自定义设置，用于在编辑器中显示/隐藏一个切换开关。切换开关是在一个 Glimmer 组件中设置的，该组件由一个初始化程序调用，以渲染在 `composer-fields` 插件出口中。

我的目标是让编辑器根据与该类别关联的自定义设置的值，在编辑器中更改类别时显示/隐藏切换开关。

是否可以在插件出口的 `outletArgs` 上添加一个观察者？编辑器模型设置在 `outletArgs` 中，所以我的想法是通过它在 `categoryId` 上设置观察者。然后，我将运行一个函数来检查类别中自定义设置的值。但是我无法正确设置观察者。这可能是因为我设置不正确，我对 EmberJS 还不熟悉。

或者有其他方法可以实现这一点吗？

作为参考，我目前运行的是 Discourse v3.4.7。

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2025年十一月14日 16:05 UTC](https://meta.discourse.org/t/triggering-a-function-whenever-the-category-is-changed-within-the-composer/388497/2 "2025-11-14T16:05:27Z")

</div>

我认为 [GitHub - discourse/discourse-custom-composer-placeholders](https://github.com/discourse/discourse-custom-composer-placeholders) 可以作为如何实现此功能的示例。  
它会根据类别更改撰写器中的占位符，并在更改时也能正常工作。

---

<div class="post-metadata">

### Author: ![frye\_bradley](https://avatars.discourse-cdn.com/v4/letter/f/848f3c/32.png) [@frye\_bradley](https://meta.discourse.org/u/frye_bradley)
#### Post date: [2025年十一月14日 17:55 UTC](https://meta.discourse.org/t/triggering-a-function-whenever-the-category-is-changed-within-the-composer/388497/3 "2025-11-14T17:55:07Z")

</div>

我尝试了此解决方案，但不幸的是，我正在构建的版本中的已初始化转换器列表较旧，缺少 `composer-editor-reply-placeholder`。

为了提供更多背景信息，我正在将插件功能重构为使用 Glimmer 组件，因为 `registerConnectorClass` API 已被弃用。下面的代码是最初设置的内容。

```plaintext
export default {
  initialize(container) {
    withPluginApi("1.6.0", api => {
      api.registerConnectorClass("composer-fields", "handlebars-template-name", {
        setupComponent(attrs, component) {
          const model = attrs.model;
          const controller = api.container.lookup("controller:composer");
          if (controller) {
            controller.addObserver("model.categoryId", this, function() {

              // 设置 showToggle 和 isRestricted 值的逻辑

              let props = {
                showField: showToggle && isRestricted,
              }
              component.setProperties(Object.assign(props));
            })
          }
        }
      }

```

是否有其他方法可以从 Glimmer 组件获得相同的功能？特别是观察 model 和/或 categoryId 的变化。
