diff --git a/src/app.ts b/src/app.ts index b497756..4a17bbc 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ import { reactive } from '@vue/reactivity' import { Block } from './block' import { Directive } from './directives' -import { createContext } from './context' +import { bindContextMethods, createContext } from './context' import { toDisplayString } from './directives/text' import { nextTick } from './scheduler' @@ -13,6 +13,7 @@ export const createApp = (initialData?: any) => { const ctx = createContext() if (initialData) { ctx.scope = reactive(initialData) + bindContextMethods(ctx.scope) // handle custom delimiters if (initialData.$delimiters) { diff --git a/src/context.ts b/src/context.ts index 2b10217..3fe9070 100644 --- a/src/context.ts +++ b/src/context.ts @@ -61,8 +61,18 @@ export const createScopedContext = (ctx: Context, data = {}): Context => { } }) ) + + bindContextMethods(reactiveProxy) return { ...ctx, scope: reactiveProxy } } + +export const bindContextMethods = (scope: Record) => { + for (const key of Object.keys(scope)) { + if (typeof scope[key] === 'function') { + scope[key] = scope[key].bind(scope) + } + } +}