The discussion revolves around injecting parameters from a template to a widget action in Discourse. oca is trying to pass a parameter from a {{#each}} loop in a template to an action in a widget. The goal is to have multiple widgets generated by the template, with each widget’s action knowing its corresponding parameter.
Initially, oca attempted to hack the problem by mutating the field c.notification_level directly, but this approach didn’t work as expected, as the changes weren’t saved.
However, after further research, oca discovered a possible solution by passing arguments to the action from the template, like this:
And then accessing these arguments in the action method:
api.modifyClass("component:some-component", {
actions: {
nameoftheaction(arg1, arg2, argA, argB) {
// arg1 and arg2 are the arguments passed from the template
// argA and argB are additional arguments provided by the component
}
}
})
This approach seems to work, but oca notes that they’re not entirely sure if their understanding is correct. Nonetheless, this solution may be helpful to others facing the same issue.
I was wondering if parameters could be injected from the template to an action and I couldn’t find a positive nor a negative answer in @Johani’s guide. My objective is to have multiple widgets generated by the template, and the action from each one would know the parameter of the {{#each}} loop.
An example, after selecting the category notification multichoice button:
Actually my problem could not be hacked the way presented above:
yes, the notification level was mutated but…
it was not saved. One would need to click elsewhere to save it with an other action.
By intensely searching random keywords on the github code database, I could find some structure like this one: {{d-button action=(action "up" wc) ...}}.
With some testing, I came to the conclusion it works this way:
The first arguments arg1, arg2 of the methods are the one given by the template. The remaining arguments argA, argB… may be given by the specificity of the component (for exemple, the category-notifications-button will return an integer depending on the setting the user clicks on).
I am absolutely not sure what I write is correct but it may help someone asking the same question I did.