هل يمكن حقن المعاملات من القالب إلى إجراء الودجة؟

Continuing the discussion from Developer’s guide to Discourse Themes:

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:

<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>
<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

إعجاب واحد (1)

لم أستطع العثور على أي شيء يتعلق بالمشكلة العامة، لكنني تمكنت من حل مشكلتي باستخدام

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

وهو ما يعني، على ما أظن، أنني أريد تعديل حقل c.notification_level بالقيمة التي يعيدها الزر.

في الواقع، لم يكن من الممكن اختراق مشكلتي بالطريقة المذكورة أعلاه:

  • نعم، تم تغيير مستوى الإشعارات ولكن…
  • لم يتم حفظه. ستحتاج إلى النقر في مكان آخر لحفظه بإجراء آخر.

من خلال البحث المكثف عن كلمات عشوائية في قاعدة بيانات كود GitHub، تمكنت من العثور على بنية مثل هذه:
{{d-button action=(action "up" wc) ...}}.

بعد بعض الاختبارات، توصلت إلى الاستنتاج بأن الأمر يعمل بهذه الطريقة:

  • القالب:
    {{d-button action=(action "اسالإجراء" arg1 arg2) ...}}`.
    
  • JavaScript:
    api.modifyClass("component:some-component", {
        actions: {
            nameoftheaction(arg1, arg2, argA, argB) {
                console.log(arg1);
                console.log(arg2);
                console.log(argA);
                console.log(argB);
            }
        }
    })
    

أول حجتين arg1 و arg2 في الطرق هما اللتان تُمرران بواسطة القالب. أما الحجج المتبقية argA، argB… فقد تُعطى حسب خصوصية المكون (على سبيل المثال، سيعيد زر category-notifications-button عددًا صحيحًا يعتمد على الإعداد الذي ينقر عليه المستخدم).

أنا لست متأكدًا تمامًا من صحة ما أكتبه، لكنه قد يساعد شخصًا يطرح نفس السؤال الذي طرحته.

إعجاب واحد (1)