Skip to content

Commit 167c49d

Browse files
Fry98yyx990803
andauthored
feat: bind methods to context (#74)
Co-authored-by: Evan You <[email protected]>
1 parent 06d3aa7 commit 167c49d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/app.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { reactive } from '@vue/reactivity'
22
import { Block } from './block'
33
import { Directive } from './directives'
4-
import { createContext } from './context'
4+
import { bindContextMethods, createContext } from './context'
55
import { toDisplayString } from './directives/text'
66
import { nextTick } from './scheduler'
77

@@ -13,6 +13,7 @@ export const createApp = (initialData?: any) => {
1313
const ctx = createContext()
1414
if (initialData) {
1515
ctx.scope = reactive(initialData)
16+
bindContextMethods(ctx.scope)
1617

1718
// handle custom delimiters
1819
if (initialData.$delimiters) {

src/context.ts

+10
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,18 @@ export const createScopedContext = (ctx: Context, data = {}): Context => {
6161
}
6262
})
6363
)
64+
65+
bindContextMethods(reactiveProxy)
6466
return {
6567
...ctx,
6668
scope: reactiveProxy
6769
}
6870
}
71+
72+
export const bindContextMethods = (scope: Record<string, any>) => {
73+
for (const key of Object.keys(scope)) {
74+
if (typeof scope[key] === 'function') {
75+
scope[key] = scope[key].bind(scope)
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)