From 9b2f8dd7b509e73a7b42efb0fde280ea25aece26 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Thu, 8 May 2025 18:32:13 +0530 Subject: [PATCH] fix: provide array of instances in controllers if multiple script exist (cherry picked from commit a2081da2960f35e7def0e670f396a9d82261af7d) --- frontend/src/data/script.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/frontend/src/data/script.js b/frontend/src/data/script.js index bc5c79a1..aec0d99c 100644 --- a/frontend/src/data/script.js +++ b/frontend/src/data/script.js @@ -48,6 +48,7 @@ export function getScript(doctype, view = 'Form') { function setupMultipleFormControllers(scriptStrings, document, helpers) { const controllers = {} + let parentInstanceIdx = null for (let scriptName in scriptStrings) { let script = scriptStrings[scriptName]?.script @@ -65,19 +66,34 @@ export function getScript(doctype, view = 'Form') { let { doctypeMeta } = getMeta(doctype) - // if className is not doctype name, then it is a child doctype - let isChildDoctype = className !== doctypeName - if (isChildDoctype) { - parentInstance = controllers[doctypeName] + if (!controllers[doctype]) { + controllers[doctype] = [] } - controllers[className] = setupFormController( + // if className is not doctype name, then it is a child doctype + let isChildDoctype = className !== doctypeName + + if (isChildDoctype) { + if (!controllers[doctype].length) { + console.error( + `⚠️ No class found for doctype: ${doctype}, it is mandatory to have a class for the parent doctype. it can be empty, but it should be present.`, + ) + return + } + parentInstance = controllers[doctype][parentInstanceIdx] + } else { + parentInstanceIdx = controllers[doctype].length || 0 + } + + const instance = setupFormController( FormClass, doctypeMeta, document, parentInstance, isChildDoctype, ) + + controllers[doctype].push(instance) }) } catch (err) { console.error('Failed to load form controller:', err)