Observer on "controller:edit-category.tabs" model not triggered

AI-generated summary

pirhoo is trying to observe changes to the model of the EditCategoryTabs controller but the observer callback is not triggered. Arkshine suggests that the controller has been moved and proposes an alternative approach using the saveCategory action. pirhoo responds that they would like to apply the logic as soon as the form is modified, not just during saving.

Hello,

I’m trying to observe changes to the model of the EditCategoryTabs controller but for some reasons, nothing happens:

import { observes } from "@ember-decorators/object"
import { withPluginApi } from "discourse/lib/plugin-api"

function initialize(api) {
  api.modifyClass(
    "controller:edit-category.tabs",
    (Superclass) =>
      class extends Superclass {
        @observes("model.parent_category_id")
        onParentCategoryChange() {
          console.log("Apply some logic here when parent category changes");
        }
      }
  );
}

export default {
  name: "new-category-permissions",
  initialize() {
    withPluginApi(initialize);
  },
}

I tried many things but I’m still not able to trigger the observer callback when the model changes. Anything I missed that might explain how to this correctly?

For the context, I’m using Discourse 3.5.1 built with official launcher.

Thanks for your help!

I was looking at your link, and the controller has been moved to /admin: https://github.com/discourse/discourse/blob/main/app/assets/javascripts/admin/addon/controllers/edit-category/tabs.js

I’m not sure why it doesn’t trigger. Maybe @observes doesn’t work well in modifyClass?

As an alternative, what do you think of:

@action
saveCategory(data) {
  const oldParentId = this.model.parent_category_id;
  super.saveCategory(data);
  const newParentId = this.model.parent_category_id;

  if (oldParentId !== newParentId) {
    console.log("Apply some logic here when parent category changes");
  }
}

Thanks @Arkshine for having a look! I can’t edit the post to reflect the new file location.

I thought about overriding saveCategory too but in that case I would like to apply the logic as soon as the form is modified. I even tried to observe change on the formData (without success).