Call a custom view function from a .raw.hbs template?


(Gustavo Scanferla) #1

Hello everyone :smile:

I want to know how I can add a custom function to the topic list view and call it from the topic list template.

I’m developing an up/down voting plugin (I know…) and I’ve already implemented an initial and static UI:

For now, I’ve added some HTML + CSS to the topic-list-item.raw.hbs

I’ve also successfully added and configured the ActiveRecord Reputation System gem. So I already can (programmatically) vote up/down on Topics.

Now I just need to make the up and down buttons work (of course! :laughing:).

The issue is that I cannot add any helpers to the template. I think the reason is because it’s a raw.hbs file. So I can’t add the {{action}} or the {{view}} helpers.
And I don’t even know if making one of them work would be the ideal approach.

I’m aware of the initialize function using reopen in order to change a view.
So I’m thinking in being able to call, from the template, a custom function that I’ll add to the discourse/topic-list-item view.
Is that possible and the best approach? If so, how can I do that?

I need this current step to be built in a way where I can, as a next step, call/request (or read) the voting score and display/toggle a class if the current user has already voted on the topic (just like the heart button does).

Thank you very much for your attention! :thumbsup:

Bonus question: How can I avoid having to ./launcher rebuild app (and wait several minutes) every time I change something on my plugin code?

(Sam Saffron) #2

We still have a single view per row, so you can extend the click handler there and handle it.

EG, something like:

import TopicListItemView from `discourse/views/topic-list-item`;

TopicListItemView.reopen(click: function(e){
   // your stuff

Yeah, you should be working in a dev environment

(Gustavo Scanferla) #3

Very nice, @sam! :sunglasses:
I’ll try that, but I know it will probably work from what I can read. Thank you very much! :raised_hands:

You mean following the Discourse Advanced Developer Install Guide? I was successfully using that before I started developing the plugin and using Docker locally. I guess this was a bad move, then! :see_no_evil: haha

Thanks again!

Edit: My click handler is now working perfectly. And yes, running it without Docker (following the instructions above) makes the development process much faster (of course!). It also freed up 13GB of disk space :smile: