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

**URL:** https://meta.discourse.org/t/having-a-hard-time-overriding-the-save-function-on-the-admin-badge-page/290831
**Category:** Development
**Created:** [January 8, 2024, 7:59am UTC](https://meta.discourse.org/t/having-a-hard-time-overriding-the-save-function-on-the-admin-badge-page/290831 "2024-01-08T07:59:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![piffy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/piffy/32/254198_2.png) [@piffy](https://meta.discourse.org/u/piffy)
#### Post date: [January 8, 2024, 7:59am UTC](https://meta.discourse.org/t/having-a-hard-time-overriding-the-save-function-on-the-admin-badge-page/290831/1 "2024-01-08T07:59:41Z")

</div>

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:

> <https://github.com/discourse/discourse/blob/d9affeca0a2c1c89f90203051486a16cb5409f23/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js#L150>

Discourse hbs:

> <https://github.com/discourse/discourse/blob/d9affeca0a2c1c89f90203051486a16cb5409f23/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs#l274>

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

```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???

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [January 8, 2024, 10:59am UTC](https://meta.discourse.org/t/having-a-hard-time-overriding-the-save-function-on-the-admin-badge-page/290831/2 "2024-01-08T10:59:34Z")

</div>

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](https://github.com/discourse/discourse/pull/24957) 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?

> <https://github.com/discourse/discourse/blob/d9affeca0a2c1c89f90203051486a16cb5409f23/app/assets/javascripts/discourse/app/models/badge.js#L38>

---

<div class="post-metadata">

### Author: ![piffy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/piffy/32/254198_2.png) [@piffy](https://meta.discourse.org/u/piffy)
#### Post date: [January 8, 2024, 8:53pm UTC](https://meta.discourse.org/t/having-a-hard-time-overriding-the-save-function-on-the-admin-badge-page/290831/3 "2024-01-08T20:53:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [February 7, 2024, 8:53pm UTC](https://meta.discourse.org/t/having-a-hard-time-overriding-the-save-function-on-the-admin-badge-page/290831/4 "2024-02-07T20:53:24Z")

</div>

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