diff --git a/src/components/FormHelperComponent.ts b/src/components/FormHelperComponent.ts index 05e6f24..2f3f6c7 100644 --- a/src/components/FormHelperComponent.ts +++ b/src/components/FormHelperComponent.ts @@ -1,8 +1,18 @@ import {Router} from "express"; import ApplicationComponent from "../ApplicationComponent.js"; +import FrontendToolsComponent from "./FrontendToolsComponent.js"; export default class FormHelperComponent extends ApplicationComponent { + + public async init(): Promise { + const globals = this.getApp().asOptional(FrontendToolsComponent)?.getGlobals(); + if (globals) { + globals.set('validation', () => ({})); + globals.set('previousFormData', () => ({})); + } + } + public async initRoutes(router: Router): Promise { router.use((req, res, next) => { let _validation: unknown | null; @@ -19,20 +29,11 @@ export default class FormHelperComponent extends ApplicationComponent { res.locals.previousFormData = () => { if (!_previousFormData) { const v = req.flash('previousFormData'); - _previousFormData = v.length > 0 ? v [0] : null; + _previousFormData = v.length > 0 ? v[0] : null; } return _previousFormData; }; - - let _formPrefix: string | null; - res.locals.getFormPrefix = () => { - return _formPrefix; - }; - res.locals.setFormPrefix = (formPrefix: string) => { - _formPrefix = formPrefix; - return ''; - }; next(); }); diff --git a/src/frontend/SvelteViewEngine.ts b/src/frontend/SvelteViewEngine.ts index 3a9b9dc..3e37520 100644 --- a/src/frontend/SvelteViewEngine.ts +++ b/src/frontend/SvelteViewEngine.ts @@ -242,8 +242,8 @@ export default class SvelteViewEngine extends ViewEngine { } let backendCall = output.substring(startIndex, endIndex); - if (backendCall.match(/([^()]+)\((.+?)\)/)) { - backendCall = backendCall.replace(/([^()]+)\((.+?)\)/, "'$1', `[$2]`"); + if (backendCall.match(/([^()]+)\((.*?)\)/)) { + backendCall = backendCall.replace(/([^()]+)\((.*?)\)/, "'$1', `[$2]`"); } else { backendCall = backendCall.replace(/([^()]+)(\(\))?/, "'$1'"); } @@ -309,7 +309,10 @@ export default class SvelteViewEngine extends ViewEngine { if (arg.startsWith("'")) return '"' + arg.substring(1, arg.length - 1) + '"'; return arg; }) - .map(arg => Function(`"use strict";const $locals = arguments[0];return (${arg});`)(locals)); // Uses named parameter locals + .filter(arg => arg.length > 0) + .map(arg => { + return Function(`"use strict";const $locals = arguments[0];return (${arg});`)(locals); + }); // Uses named parameter locals const f = locals[key]; if (typeof f !== 'function') throw new Error(key + ' is not a function.');