How to Modify Ember Route Class

AI-generated summary

In a discussion titled ‘How to Modify Ember Route Class’ under the Dev category, saurabhmasc asked if Ember Route classes can be modified using api.modifyClass similarly to Controller and Component classes. justin confirmed it’s possible using route:route-name. When saurabhmasc tried overriding the createTopic action in route:tags-show, they encountered a ReferenceError: Composer is not defined. Johani explained that since theme scripts can’t use import, Composer must be loaded via require('discourse/models/composer'). They also recommended reading Splitting Up Theme JavaScript Into Multiple Files for better theme development practices, as it aligns more closely with core Discourse examples and improves maintainability.

Hello All,
I want to modify the Ember Route class (/app/assets/javascripts/discourse/routes/tags-show.js.es6) in the same way that we modify Controller and Component classes using pluginAPI.
An example that we modify composer.js.es6 controller as below:

<script type="text/discourse-plugin" version="0.8">
  api.modifyClass('controller:composer', {
    actions: {
        newActionHere() { }
   }
});
</script>

Is it possible to modify the Ember Route class in the same way?

I believe this is possible — I think it works via route:route-name. Have you given it a try?

route:route-name working but when I try to override action createTopic as shown below:

<script type="text/discourse-plugin" version="0.8">
  api.modifyClass('route:tags-show', {
    actions: {
     createTopic() {
      const controller = this.controllerFor("tags.show");

      if (controller.get("list.draft")) {
        this.openTopicDraft(controller.get("list"));
      } else {
        this.controllerFor("composer")
          .open({
            categoryId: controller.get("category.id"),
            action: Composer.CREATE_TOPIC,
            draftKey: controller.get("list.draft_key"),
            draftSequence: controller.get("list.draft_sequence")
          })
          .then(() => {
            // Pre-fill the tags input field
            if (controller.get("model.id")) {
              const composerModel = this.controllerFor("composer").get("model");

              composerModel.set(
                "tags",
                _.compact(
                  _.flatten([
                    controller.get("model.id"),
                    controller.get("additionalTags")
                  ])
                )
              );
            }
          });
      }
    }
}
 });
</script>   

It throws Error :
ReferenceError: Composer is not defined

This happens because Composer is not defined in your code. If you look at the tags-show route, you’ll see that Composer is defined at the top.

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/routes/tags-show.js.es6#L2

However, since you’re doing this in theme script tags, you can’t use import. You’ll have to use require instead.

So, it should work if you add something like this to the top of your code

const Composer = require("discourse/models/composer");

That said, I strongly recommend that you spend a bit of time reading

and experimenting with this new way of creating themes. It’s much easier to follow examples in core that way.