Having a hard time overriding the save() function on the admin badge page

I’m writing a plugin to extend badge functionality. When the “save” button is pressed on the admin badge page, I want to execute some additional code.

Discourse Javascript:

Discourse hbs:

My code discourse/plugins/discourse-badge-extension/assets/javascripts/discourse/initializers/modify-admin-badges-show-controller.js:

import { withPluginApi } from 'discourse/lib/plugin-api';

export default {
  name: 'modify-admin-badges-show',

  initialize() {

    withPluginApi('0.8', api => {
      api.modifyClass('controller:admin-badges/show', {
        pluginId: 'discourse-badge-extension',
        actions: {

          save() {
            console.log("Custom pre-save logic triggered");
            alert("Pre-save logic");

            this._super(...arguments); // Call the original save method

            console.log("Post-save logic triggered");
            alert("Post-save logic");
          },
        }
      });

      alert("Hello! I am an alert box!!");  
    });
  }
};

It’s an initializer.

  • The alert box "Hello! I am an alert box!!" is triggered, indicating the code is being run on reload.
  • Neither the console.log lines nor the alerts run on saving, indicating the overriding is not working?
  • the controller name does not give an error controller:admin-badges/show (but intentionally adding a typo does, suggesting I am targeting a real controller)

It should be easy to repo given it’s just one initializer. What am I doing wrong???

Hey @piffy. Unfortunately this is an incompatibility with JS ‘Native class syntax’, EmberObjects’s reopen() functionality, and the @action decorator (which technically, creates a native JS ‘getter’).

I’m afraid I don’t have an immediate suggestion to get this particular override working, but we are working on a more robust solution for the future.

Depending on what you’re trying to do, maybe hooking into the badge model’s “save” function could work?

3 Likes

Thank you for the fast and comprehensive answer. I will look into that other save function.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.