Overwrite methods in Discourse.Utilities

I’m struggling with EmberJS for a while now. I need to override Discourse.Utilities.getUploadMarkdown in a plugin, but i’m not getting anywhere. How do you override export methods in Discourse.Utilities. any help plz?

1 Like

I did it in this plugin a while ago. https://github.com/scossar/m4a-audio-uploads

7 Likes

Hi Simon, thanks for your reply. This is exactly what I want. But upon following your javascript code, I get error that Discourse.Utilities is undefined.
Uncaught TypeError: Cannot set property 'getUploadMarkdown' of undefined
and it stills calls the old getUploadMarkdown method.

Yes, it looks like Discourse.Utilities has been converted to javascript modules since I wrote that plugin, so it will have to be done differently.

3 Likes

Still no luck. I’m unable to find a workaround to overwrite getUploadMarkdown. It won’t let me overwrite it. getUploadMarkdown is read-only. Any help?

I spent some time trying to figure this out and didn’t find an obvious way to do it. What makes sense to me is reopening the composer-editor component and making the changes there. It would be possible to override the _bindUploadTarget function that calls getUploadMarkdown, but that seems like a bad idea.

If getUploadMarkdown was called inside of a function in composer-editor.js.es6, it would be possible to do something like this in a site customization - for testing this I removed wav files from the audio file list:


// composer-editor.js.es6
...
_getUploadMarkdown(upload) {
    return getUploadMarkdown(upload);
},
...

<script>
    var ComposerEditor = require('discourse/components/composer-editor').default;
    var uploadLocation = require('discourse/lib/utilities').uploadLocation;
    
    ComposerEditor.reopen({
        _getUploadMarkdown: function(upload) {
            if (!Discourse.SiteSettings.prevent_anons_from_downloading_files && (/\.(wav)$/i).test(upload.original_filename)) {
                
                return uploadLocation(upload.url);
            }
            
            return this._super(upload);
        }
    }); 
</script>

Maybe there’s a way to do something similar by doing something directly with the getUploadMarkdown import.

1 Like

Thanks for your efforts Simon. Yeah, I ended up overriding _bindUploadTarget and defined getCustomUploadMarkdown. And it works fine for now.

ComposerEditor.reopen({
_bindUploadTarget() {

const markdown = getCustomUploadMarkdown(upload);

}
)};

1 Like