Can we generate programmatically field triggers and handlers for client-side javascript?
My code is working. The thing is, I have to list down each field name for the trigger and the same function handler.
I was wondering if I can traverse my field_names array of affected fields and generate trigger and handler, instead of listing each field name and placing the same handler like using javascript array forEach method.
// Copyright (c) 2020, Joseph Marie Alba and contributors
// For license information, please see license.txt
frappe.ui.form.on('Acam Factor', {
validate: function (frm) {
compute_check_factors(frm)
if (frm.doc.check_factors && frm.doc.check_factors != 100)
frappe.throw(__("Please ensure Check Factor is 100"));
},
distribution_services: function (frm) {
compute_check_factors(frm)
},
distribution_connection_services: function (frm) {
compute_check_factors(frm)
},
regulated_retail_services: function (frm) {
compute_check_factors(frm)
},
non_regulated_retail_services: function (frm) {
compute_check_factors(frm)
},
supplier_of_last_resort: function (frm) {
compute_check_factors(frm)
},
wholesale_aggregator: function (frm) {
compute_check_factors(frm)
},
related_business: function (frm) {
compute_check_factors(frm)
},
generation: function (frm) {
compute_check_factors(frm)
},
supply_services: function (frm) {
compute_check_factors(frm)
},
general_purpose: function (frm) {
compute_check_factors(frm)
}
})
var compute_check_factors = function (frm) {
let field_names = [
"distribution_services",
"distribution_connection_services",
"regulated_retail_services",
"non_regulated_retail_services",
"supplier_of_last_resort",
"wholesale_aggregator",
"related_business",
"generation",
"supply_services",
"general_purpose"
]
field_names.forEach((field_name) => (frm.doc[field_name] = frm.doc[field_name] ? flt(frm.doc[field_name]) : 0.000 ))
frm.doc.check_factors = field_names.map(field_name => frm.doc[field_name]).reduce((total, value) => total + value)
frm.refresh_field(field_names)
frm.refresh_field('check_factors')
}