Skip to content

Commit

Permalink
feat: bind methods to context (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan You <yyx990803@gmail.com>
  • Loading branch information
Fry98 and yyx990803 committed Sep 14, 2021
1 parent 06d3aa7 commit 167c49d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,18 @@ export const createScopedContext = (ctx: Context, data = {}): Context => {
}
})
)

bindContextMethods(reactiveProxy)
return {
...ctx,
scope: reactiveProxy
}
}

export const bindContextMethods = (scope: Record<string, any>) => {
for (const key of Object.keys(scope)) {
if (typeof scope[key] === 'function') {
scope[key] = scope[key].bind(scope)
}
}
}

0 comments on commit 167c49d

Please sign in to comment.