From 167c49d6940c6f35c6002093d8807ac0e835dcea Mon Sep 17 00:00:00 2001 From: Filip Toman <31852506+Fry98@users.noreply.github.com> Date: Tue, 14 Sep 2021 17:43:04 +0200 Subject: [PATCH] feat: bind methods to context (#74) Co-authored-by: Evan You --- src/app.ts | 3 ++- src/context.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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) + } + } +}