Skip to content

Commit ec11d59

Browse files
authored
🐛 Allows Class methods/props to be exposed as the root data component (#4038)
1 parent 9501ba4 commit ec11d59

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

packages/alpinejs/src/scope.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let mergeProxyTrap = {
4848
if (name == Symbol.unscopables) return false;
4949

5050
return objects.some((obj) =>
51-
Object.prototype.hasOwnProperty.call(obj, name)
51+
Reflect.has(obj, name)
5252
);
5353
},
5454

@@ -57,7 +57,7 @@ let mergeProxyTrap = {
5757

5858
return Reflect.get(
5959
objects.find((obj) =>
60-
Object.prototype.hasOwnProperty.call(obj, name)
60+
Reflect.has(obj, name)
6161
) || {},
6262
name,
6363
thisProxy
@@ -66,7 +66,7 @@ let mergeProxyTrap = {
6666

6767
set({ objects }, name, value, thisProxy) {
6868
const target = objects.find((obj) =>
69-
Object.prototype.hasOwnProperty.call(obj, name)
69+
Reflect.has(obj, name)
7070
) || objects[objects.length - 1];
7171
const descriptor = Object.getOwnPropertyDescriptor(target, name);
7272
if (descriptor?.set && descriptor?.get)

tests/cypress/integration/scope.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,32 @@ test(
5656
get("span#two").should(haveText("foobar"));
5757
}
5858
);
59+
60+
test(
61+
"allows accessing class methods",
62+
[
63+
html`
64+
<script>
65+
class Counter {
66+
value = 0;
67+
constructor() {}
68+
increment() {
69+
this.value++;
70+
}
71+
}
72+
document.addEventListener("alpine:init", () =>
73+
Alpine.data("counter", () => new Counter())
74+
)
75+
</script>
76+
<div x-data="counter">
77+
<button type="button" @click="increment" x-text=value></button>
78+
</div>
79+
`,
80+
],
81+
({ get }) => {
82+
get("button").should(haveText("0"));
83+
get("button").click();
84+
get("button").should(haveText("1"));
85+
}
86+
);
87+

0 commit comments

Comments
 (0)