What is the proper function to initalize stuff on Composer Open?

AI-generated summary

spirobel is seeking the correct function to initialize a project_task object whenever the Composer is opened, ensuring it’s always available. They initially used a method inspired by the events plugin but found it unreliable, as the function doesn’t trigger consistently when switching between drafts or topics. Their current workaround involves forcing draft discard upon editing a different topic, which they acknowledge is imperfect and may lead to unintended draft loss. They note that the Composer’s behavior—allowing only one active edit draft at a time—is intentional, as confirmed by a related meta-discourse thread. spirobel initially viewed the lack of warning as a bug but now understands it as expected behavior.

I have a function to set up some object called project_task every time the composer is opened.

I did it this way because I saw it in the events plugin:

https://github.com/paviliondev/discourse-events/blob/master/assets/javascripts/discourse/connectors/composer-fields/composer-controls-event.js.es6

the issue that i have now is, that my object is not always there. It seems like the function does not always run when the composer changes. If i switch back and forth between different drafts of different topics its not called again, so the code that depends on the object being there does not work anymore.
My question is: which function do I need to use if I want to setup this object anytime the composer is opened with a different topic or if its opened for the first time? I want to make sure this object is always there.

EDIT:
This is how I solved the problem for now

But this is still not perfect, because it forces us to throw away the draft when we press edit on another
topic. sometimes without warning. (maybe that is because my saving to draft stuff does not work 100% perfectly or I just clicked too fast and the draft wasnt saved yet)
I guess this is the default behavior of the composer at the moment. I just verified in this forum, that you can only have one edit draft at one time.


When I press new topic in this forum then it lets me save stuff, so there seems to be the option to have one edit draft and one new draft.

I found this thread again:

It seems like this is the behavior of the composer how its intended.
To me this seems really intuitive and thats why I thought its a bug. I didnt get the error message at first when it forced me to discard or cancel, because it didnt even occur to me that there could be a limitation on open drafts.

2 Likes