Dev question - setTopicController event removed.

Quick question guys:

We were using

 TopicRoute.on("setupTopicController", function(event) {
   topicController = event.controller
 })

in a plugin’s -init.js to get access to the postStream’s “hasNoFilters” attribute.
This has been removed in commit 6381170 (DEV: Remove `setupTopicController` event · discourse/discourse@6381170 · GitHub), and now we’re not sure how to get at that attribute anymore.

Full context:

import { withPluginApi } from 'discourse/lib/plugin-api'
import TopicRoute from 'discourse/routes/topic'

function initializePlugin(api) {
let topicController;

  TopicRoute.on("setupTopicController", function(event) {
    topicController = event.controller
  })

  api.addPostMenuButton('iso', attrs => {

    return {
      action: 'clickIso',
      icon: topicController.get("model.postStream.hasNoFilters") ? 'user' : 'users',
      title: topicController.get("model.postStream.hasNoFilters") ? 'iso.title' : 'unisoed.title',
      position: 'second'
    }
  })

  api.attachWidgetAction('post-menu', 'clickIso', function() {
      if(topicController.get("model.postStream.hasNoFilters")) {
        const postStream = topicController.get("model.postStream");
        postStream.toggleParticipant(this.attrs.username)
          .then(() => postStream.refresh())
          .then(() => topicController.send("jumpToPostId", this.attrs.id));
      }
      else {
        const postStream = topicController.get("model.postStream");
        postStream.cancelFilter();
        postStream.refresh()
          .then(() => topicController.send("jumpToPostId", this.attrs.id));
      }
  })
}

export default {
  name: 'iso-button',
  initialize: function() {
    withPluginApi('0.8.6', api => initializePlugin(api))
  }
}
1 Like

Replace the replace the TopicRoute.on block with this

TopicRoute.reopen({
    setupController(controller, model) {
      this._super(controller, model);
      topicController = controller;
    }
  });

Should work but there should be a better way to handle this for sure I guess.

js is maybe one of the few or the only language such a thing works.

1 Like

Hmm I made the replacement and it’s still broken, will look into further.

1 Like

Made a PR here, with a bit of explanation of what’s going on:

2 Likes

Interesting. So resolver gives out an instance and not a class? Interesting.

Also, we could simplify this code further by using api._lookupContainer?

1 Like

Yup, that’s right. (although technically ember doesn’t have classes, it has “Factories”. This is why you’ll see lookupFactory and function names like that).

Yup, good point. Technically it’s not part of the api, but that’s a better simpler approach than using the container passed to the initializer.

1 Like