In the Dev category, stevenpslade sought a method to retrieve a topic’s category ID for JavaScript usage, referencing the Discourse.TopicRoute.on event. joebuhlig responded by sharing an implementation from their feature voting plugin, demonstrating that the ID can be accessed via topic.category.id after retrieving the topic object. stevenpslade confirmed their understanding that reopening the TopicRoute allows accessing the model with this.modelFor('topic') to achieve this. joebuhlig validated this approach, linking to their Discourse Feature Voting topic as further proof of the method’s effectiveness.
I’m looking to get a topic’s category id to use in javascript. Since this info is already being pulled to grab the category tag in each topic for handlebars, I was wondering if I could use that somehow?
A likely helpful piece of code could be this:
Discourse.TopicRoute.on("setupTopicController", function(event) { // Access the controller via: event.controller // Access the model via: event.currentModel });
Thanks for this @joebuhlig. So you think if you reopen the TopicRoute, we should be ble to get the topic with var topic = this.modelFor('topic'); and then the rest with topic.category.id?