# Can parameters be injected from the template to a widget action?

**URL:** https://meta.discourse.org/t/can-parameters-be-injected-from-the-template-to-a-widget-action/155275
**Category:** Development
**Created:** [June 18, 2020, 9:09pm UTC](https://meta.discourse.org/t/can-parameters-be-injected-from-the-template-to-a-widget-action/155275 "2020-06-18T21:09:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![oca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/oca/32/156060_2.png) [@oca](https://meta.discourse.org/u/oca)
#### Post date: [June 18, 2020, 9:09pm UTC](https://meta.discourse.org/t/can-parameters-be-injected-from-the-template-to-a-widget-action/155275/1 "2020-06-18T21:09:39Z")

</div>

Continuing the discussion from [Developer’s guide to Discourse Themes](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648):

Dear all,

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:

```js
<script type="text/x-handlebars" data-template-name="components/some_component">
{{#each categories as |c|}}
    {{category-notifications-button
        value=c.notification_level
        category=c
        onChange=(action "changeCategoryNotificationLevel" ***somehow_inject_c_as_a_parameter*** )
    }}
{{/each}}
</script>

```

```js
<script type="text/discourse-plugin" version="0.8">
  api.modifyClass("controller:some_component", {
    actions: {
      changeCategoryNotificationLevel(selected_value, ***c_as_a_parameter*** ) {
        c_as_a_parameter.setNotification(notificationLevel);
      }
    }
  }
});
</script>

```

Is there a way, inside `changeCategoryNotificationLevel` to know which category I’m in from the template ?

If not, if my problem is well is described well enough, can anyone see an other way to achieve the same outcome ?

Thanks in advance

---

<div class="post-metadata">

### Author: ![oca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/oca/32/156060_2.png) [@oca](https://meta.discourse.org/u/oca)
#### Post date: [June 22, 2020, 5:11pm UTC](https://meta.discourse.org/t/can-parameters-be-injected-from-the-template-to-a-widget-action/155275/2 "2020-06-22T17:11:44Z")

</div>

I couldn’t find anything about the general problem, but I could hack my problem with

```js
{{#each categories as |c|}}
    {{category-notifications-button
        ...
        onChange=(action (mut c.notification_level))
    }}
{{/each}}

```

which mean, I guess, I want to mutate the field `c.notification_level` with the value returned by the button.

---

<div class="post-metadata">

### Author: ![oca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/oca/32/156060_2.png) [@oca](https://meta.discourse.org/u/oca)
#### Post date: [July 5, 2020, 7:49am UTC](https://meta.discourse.org/t/can-parameters-be-injected-from-the-template-to-a-widget-action/155275/3 "2020-07-05T07:49:44Z")

</div>

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:

- template:

```hbs
{{d-button action=(action "nameoftheaction" arg1 arg2) ...}}`.

```

- JS:

```js
api.modifyClass("component:some-component", {
    actions: {
        nameoftheaction(arg1, arg2, argA, argB) {
            console.log(arg1);
            console.log(arg2);
            console.log(argA);
            console.log(argB);
        }
    }
})

```

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.
