Hook for a topic's category id?

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 });

Can anyone help me with this?

Here’s how I’m doing it in the feature voting plugin:

https://github.com/joebuhlig/discourse-feature-voting/blob/master/assets/javascripts/initializers/feature-voting.js.es6#L10

You get the topic first and then (if I remember correctly) you can do this:

topic.category.id

PS. I believe with the plugin changes, I may have some updates to do to this file.

2 Likes

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?

That’s how I’m doing it here: