Hook for a topic's category id?

AI 生成的摘要

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

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.

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: