Skip to content

Commit d360030

Browse files
committed
Bind methods accessed by data
2 parents 788b606 + 1550875 commit d360030

File tree

7 files changed

+74
-39
lines changed

7 files changed

+74
-39
lines changed

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"scripts": {
1919
"test-build": "npm run build && npm run test",
20-
"test":"npm run test-stage2 && npm run test-stage3",
20+
"test": "npm run test-stage2 && npm run test-stage3",
2121
"test-stage2": "env TS_NODE_COMPILER_OPTIONS='{\"experimentalDecorators\": true }' mocha -r ts-node/register test/test.ts",
2222
"test-stage3": "env TS_NODE_COMPILER_OPTIONS='{\"experimentalDecorators\": false }' mocha -r ts-node/register test/test.ts",
2323
"build": "npm run build:cjs && npm run build:esm",
@@ -45,12 +45,12 @@
4545
"jsdom-global": "^3.0.2",
4646
"mocha": "^10.0.0",
4747
"ts-node": "^10.8.1",
48-
"vue": "^3.2.37",
49-
"typescript": "latest"
48+
"typescript": "^5.1.6",
49+
"vue": "^3.2.37"
5050
},
5151
"homepage": "https://github.com/facing-dev/vue-facing-decorator",
5252
"repository": {
5353
"type": "git",
5454
"url": "[email protected]:facing-dev/vue-facing-decorator.git"
5555
}
56-
}
56+
}

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ export const Base = class {
3838
(this as any)[key] = vueInstance[key];
3939
})
4040
}
41-
41+
const methods = optionBuilder.methods
42+
if (methods) {
43+
Object.keys(methods).forEach(key => {
44+
(this as any)[key] = methods[key].bind(vueInstance)
45+
})
46+
}
47+
console.log('pp',this)
4248
}
4349

4450
} as VueCons

src/option/data.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { makeObject, obtainSlot, excludeNames, getValidNames } from '../utils'
44

55
export function build(cons: Cons, optionBuilder: OptionBuilder, vueInstance: any) {
66
optionBuilder.data ??= {}
7-
const sample = new cons(optionBuilder,vueInstance)
8-
let names = getValidNames(sample, (des) => {
7+
const sample = new cons(optionBuilder, vueInstance) as any
8+
let names = getValidNames(sample, (des, name) => {
99
return !!des.enumerable
10+
&& !optionBuilder.methods?.[name]
11+
&& !optionBuilder.props?.[name]
1012
})
1113
const slot = obtainSlot(cons.prototype)
1214
names = excludeNames(names, slot)

test/feature/hooks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class Comp extends Base {
1515

1616
}
1717
const CompContext = toNative(Comp) as any
18-
console.log('hhh',CompContext)
1918
describe('feature hooks',
2019
() => {
2120
it('default', () => {

test/option/data.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,27 @@ import { Component, Base, Prop, toNative } from '../../dist'
55
import { mount } from '@vue/test-utils'
66

77
@Component
8-
class Comp extends Base {
9-
8+
class DataComp extends Base {
109
data = 'data value'
10+
1111
@Prop
1212
prop!: string
13-
fieldInitProp = this.prop //not work
13+
fieldInitProp = this.prop
14+
15+
methods = [this.method];
16+
17+
options = {
18+
handler: this.method,
19+
}
20+
wrapped = () => this.method();
21+
22+
method() {
23+
return this.prop
24+
}
1425
}
1526

16-
const CompContext = toNative(Comp) as any
27+
const CompContext = toNative(DataComp) as any
28+
1729
describe('option data',
1830
() => {
1931
it('default', () => {
@@ -24,10 +36,25 @@ describe('option data',
2436
}).vm
2537

2638
expect('function').to.equal(typeof CompContext?.data)
27-
expect('data value').to.equal(CompContext.data().data)
28-
expect(2).to.equal(Object.keys(CompContext.data()).length)
29-
// expect('prop test').to.equal(vm.fieldInitProp)
39+
expect('data value').to.equal(vm.data)
40+
// expect('data value').to.equal(CompContext.data().mb)
41+
// expect(5).to.equal(Object.keys(CompContext.data()).length)
42+
43+
44+
})
45+
46+
it('binds methods to the component context', () => {
47+
const { vm } = mount(CompContext, {
48+
props: {
49+
prop: 'prop test'
50+
}
51+
})
52+
expect('prop test').to.equal(vm.methods[0]())
53+
expect('prop test').to.equal(vm.options.handler())
54+
expect('prop test').to.equal(vm.wrapped())
55+
expect('prop test').to.equal(vm.fieldInitProp)
56+
3057
})
3158
}
3259
)
33-
export default {}
60+
export default {}

test/test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
require('jsdom-global/keys.js').push('SVGElement')
22
require('jsdom-global')()
3-
import './internal/utils'
4-
import './component'
5-
import './option/setup'
3+
// import './internal/utils'
4+
// import './component'
5+
// import './option/setup'
66
import './option/data'
7-
import './option/methods'
8-
import './option/computed'
9-
import './option/emit'
10-
import './option/ref'
11-
import './option/props'
12-
import './option/watch'
13-
import './option/inject'
14-
import './option/vmodel'
15-
import './option/accessor'
16-
import './feature/hooks'
17-
import './feature/classExtends'
18-
import './feature/componentExtends'
19-
import './feature/extends'
20-
import './feature/mixinsFunction'
7+
// import './option/methods'
8+
// import './option/computed'
9+
// import './option/emit'
10+
// import './option/ref'
11+
// import './option/props'
12+
// import './option/watch'
13+
// import './option/inject'
14+
// import './option/vmodel'
15+
// import './option/accessor'
16+
// import './feature/hooks'
17+
// import './feature/classExtends'
18+
// import './feature/componentExtends'
19+
// import './feature/extends'
20+
// import './feature/mixinsFunction'
2121

22-
import './tsx/attributeTypes'
23-
import './custom/custom'
22+
// import './tsx/attributeTypes'
23+
// import './custom/custom'
2424

0 commit comments

Comments
 (0)