Skip to content

Commit 9aafc00

Browse files
committed
Merge branch 'reedsy-method-from-data-this'
2 parents 70c9b74 + ec111ae commit 9aafc00

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ export const Base = class {
3939
(this as any)[key] = vueInstance[key];
4040
})
4141
}
42-
42+
const methods = optionBuilder.methods
43+
if (methods) {
44+
Object.keys(methods).forEach(key => {
45+
(this as any)[key] = methods[key].bind(vueInstance)
46+
})
47+
}
4348
}
4449

4550
} as VueCons
4651

4752
export const Vue = Base
4853

49-
export { toNative } from './component'
54+
export { toNative } from './component'

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,(mapName) => {

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-
1918
describe('feature hooks',
2019
() => {
2120
it('default', () => {

test/option/data.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,30 @@ import 'mocha';
44
import { Component, Base, Prop, toNative } from '../../dist'
55
import { mount } from '@vue/test-utils'
66

7-
@Component({template: '<div/>'})
8-
class Comp extends Base {
97

8+
@Component({ template: '<div/>' })
9+
class Comp extends Base {
1010
data = 'data value'
11+
1112
@Prop
1213
prop!: string
14+
1315
fieldInitProp = this.prop //not work
16+
17+
methods = [this.method];
18+
19+
options = {
20+
handler: this.method,
21+
}
22+
wrapped = () => this.method();
23+
24+
method() {
25+
return this.prop
26+
}
1427
}
1528

1629
const CompContext = toNative(Comp) as any
30+
1731
describe('option data',
1832
() => {
1933
it('default', () => {
@@ -24,9 +38,22 @@ describe('option data',
2438
}).vm
2539

2640
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)
41+
expect('data value').to.equal(vm.data)
42+
expect(5).to.equal(Object.keys(CompContext.data()).length)
43+
})
44+
45+
it('binds methods to the component context', () => {
46+
const { vm } = mount(CompContext, {
47+
props: {
48+
prop: 'prop test'
49+
}
50+
})
51+
expect('prop test').to.equal(vm.methods[0]())
52+
expect('prop test').to.equal(vm.options.handler())
53+
expect('prop test').to.equal(vm.wrapped())
2954
// expect('prop test').to.equal(vm.fieldInitProp)
55+
56+
3057
})
3158
}
3259
)

test/test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import './option/ref'
1111
import './option/props'
1212
import './option/watch'
1313
import './option/inject'
14-
import './option/provide'
1514
import './option/vmodel'
1615
import './option/accessor'
1716
import './feature/hooks'
1817
import './feature/classExtends'
1918
import './feature/componentExtends'
2019
import './feature/extends'
2120
import './feature/mixinsFunction'
22-
2321
import './tsx/attributeTypes'
2422
import './custom/custom'
2523

0 commit comments

Comments
 (0)