fix: pass getDoc function instead of document.doc to keep the reactivity

(cherry picked from commit db0c0d98bc88710fc8377204e7a927627f9fe8d6)
This commit is contained in:
Shariq Ansari 2025-05-26 16:46:24 +05:30 committed by Mergify
parent b3c1ebb24d
commit d820ff1572

View File

@ -127,9 +127,10 @@ export function getScript(doctype, view = 'Form') {
} }
setupHelperMethods(FormClass, document) setupHelperMethods(FormClass, document)
const getDoc = () => document.doc
if (isChildDoctype) { if (isChildDoctype) {
instance.doc = createDocProxy(document.doc, parentInstance, instance) instance.doc = createDocProxy(getDoc, parentInstance, instance)
if (!parentInstance._childInstances) { if (!parentInstance._childInstances) {
parentInstance._childInstances = [] parentInstance._childInstances = []
@ -137,22 +138,21 @@ export function getScript(doctype, view = 'Form') {
parentInstance._childInstances.push(instance) parentInstance._childInstances.push(instance)
} else { } else {
instance.doc = createDocProxy(document.doc, instance) instance.doc = createDocProxy(getDoc, instance)
} }
return instance return instance
} }
function setupHelperMethods(FormClass, document) { function setupHelperMethods(FormClass) {
if (typeof FormClass.prototype.getRow !== 'function') { if (typeof FormClass.prototype.getRow !== 'function') {
FormClass.prototype.getRow = function (parentField, idx) { FormClass.prototype.getRow = function (parentField, idx) {
let data = document.doc
idx = idx || this.currentRowIdx idx = idx || this.currentRowIdx
let dt = null let dt = null
if (this instanceof Array) { if (this instanceof Array) {
const { getFields } = getMeta(data.doctype) const { getFields } = getMeta(this.doc.doctype)
let fields = getFields() let fields = getFields()
let field = fields.find((f) => f.fieldname === parentField) let field = fields.find((f) => f.fieldname === parentField)
dt = field?.options?.replace(/\s+/g, '') dt = field?.options?.replace(/\s+/g, '')
@ -162,13 +162,13 @@ export function getScript(doctype, view = 'Form') {
} }
} }
if (!data[parentField]) { if (!this.doc[parentField]) {
console.warn( console.warn(
__('⚠️ No data found for parent field: {0}', [parentField]), __('⚠️ No data found for parent field: {0}', [parentField]),
) )
return null return null
} }
const row = data[parentField].find((r) => r.idx === idx) const row = this.doc[parentField].find((r) => r.idx === idx)
if (!row) { if (!row) {
console.warn( console.warn(
@ -180,7 +180,7 @@ export function getScript(doctype, view = 'Form') {
return null return null
} }
row.parent = row.parent || data.name row.parent = row.parent || this.doc.name
if (this instanceof Array && dt) { if (this instanceof Array && dt) {
return createDocProxy( return createDocProxy(
@ -223,43 +223,70 @@ export function getScript(doctype, view = 'Form') {
return FormClass return FormClass
} }
function createDocProxy(data, instance, childInstance = null) { function createDocProxy(source, instance, childInstance = null) {
return new Proxy(data, { const isFunction = typeof source === 'function'
get(target, prop) { const getCurrentData = () => (isFunction ? source() : source)
if (prop === 'trigger') {
if ('trigger' in data) { return new Proxy(
console.warn( {},
__( {
'⚠️ Avoid using "trigger" as a field name — it conflicts with the built-in trigger() method.', get(target, prop) {
), const currentDocData = getCurrentData()
if (!currentDocData) return undefined
if (prop === 'trigger') {
if (currentDocData && 'trigger' in currentDocData) {
console.warn(
__(
'⚠️ Avoid using "trigger" as a field name — it conflicts with the built-in trigger() method.',
),
)
}
return (methodName, ...args) => {
const method = instance[methodName]
if (typeof method === 'function') {
return method.apply(instance, args)
} else {
console.warn(
__('⚠️ Method "{0}" not found in class.', [methodName]),
)
}
}
}
if (prop === 'getRow') {
return instance.getRow.bind(
childInstance || instance._childInstances || instance,
) )
} }
return (methodName, ...args) => { return currentDocData[prop]
const method = instance[methodName] },
if (typeof method === 'function') { set(target, prop, value) {
return method.apply(instance, args) const currentDocData = getCurrentData()
} else { if (!currentDocData) return false
console.warn(
__('⚠️ Method "{0}" not found in class.', [methodName]),
)
}
}
}
if (prop === 'getRow') { currentDocData[prop] = value
return instance.getRow.bind( return true
childInstance || instance._childInstances || instance, },
) has(target, prop) {
} const currentDocData = getCurrentData()
if (!currentDocData) return false
return target[prop] return prop in currentDocData
},
ownKeys(target) {
const currentDocData = getCurrentData()
if (!currentDocData) return []
return Reflect.ownKeys(currentDocData)
},
getOwnPropertyDescriptor(target, prop) {
const currentDocData = getCurrentData()
if (!currentDocData) return undefined
return Reflect.getOwnPropertyDescriptor(currentDocData, prop)
},
}, },
set(target, prop, value) { )
target[prop] = value
return true
},
})
} }
return { return {