Refactor component event injection to handle missing event selectors

This commit is contained in:
Max 2024-07-29 16:46:21 +08:00
parent a91e47b072
commit dc3c637c5e
2 changed files with 11 additions and 9 deletions

View file

@ -684,7 +684,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component
arguments := "document.body"
if !ispage {
arguments = "arguments[0]"
arguments = "component"
}
injectScript := componentInitScript(arguments)
@ -754,7 +754,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component
parent := "body"
if !ispage {
parent = "head"
code = fmt.Sprintf("function %s(){\n%s\n}\n", component, addTabToEachLine(code))
code = fmt.Sprintf("function %s( component ){\n%s\n}\n", component, addTabToEachLine(code))
}
scripts = append(scripts, ScriptNode{

View file

@ -216,12 +216,14 @@ const pageEventScriptTmpl = `
`
const compEventScriptTmpl = `
document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) {
const dataKeys = %s;
const jsonKeys = %s;
handler = new %s(this).%s;
__sui_event_handler(event, dataKeys, jsonKeys, this, handler);
});
if (document.querySelector("[s\\:event=%s]")) {
document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) {
const dataKeys = %s;
const jsonKeys = %s;
handler = new %s(this).%s;
__sui_event_handler(event, dataKeys, jsonKeys, this, handler);
});
}
`
const componentInitScriptTmpl = `
@ -262,7 +264,7 @@ func pageEventInjectScript(eventID, eventName, dataKeys, jsonKeys, handler strin
}
func compEventInjectScript(eventID, eventName, component, dataKeys, jsonKeys, handler string) string {
return fmt.Sprintf(compEventScriptTmpl, eventID, eventName, dataKeys, jsonKeys, component, handler)
return fmt.Sprintf(compEventScriptTmpl, eventID, eventID, eventName, dataKeys, jsonKeys, component, handler)
}
func componentInitScript(root string) string {