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

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: discourse/app/assets/javascripts/admin/addon/controllers/edit-category/tabs.js at main · discourse/discourse · GitHub

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");
  }
}
2 Likes

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).