Explorar el Código

:bug: Allows Class methods/props to be exposed as the root data component (#4038)

Eric Kwoka hace 1 año
padre
commit
ec11d59e22
Se han modificado 2 ficheros con 32 adiciones y 3 borrados
  1. 3 3
      packages/alpinejs/src/scope.js
  2. 29 0
      tests/cypress/integration/scope.spec.js

+ 3 - 3
packages/alpinejs/src/scope.js

@@ -48,7 +48,7 @@ let mergeProxyTrap = {
         if (name == Symbol.unscopables) return false;
 
         return objects.some((obj) =>
-            Object.prototype.hasOwnProperty.call(obj, name)
+            Reflect.has(obj, name)
         );
     },
 
@@ -57,7 +57,7 @@ let mergeProxyTrap = {
 
         return Reflect.get(
             objects.find((obj) =>
-                Object.prototype.hasOwnProperty.call(obj, name)
+                Reflect.has(obj, name)
             ) || {},
             name,
             thisProxy
@@ -66,7 +66,7 @@ let mergeProxyTrap = {
 
     set({ objects }, name, value, thisProxy) {
         const target = objects.find((obj) =>
-                Object.prototype.hasOwnProperty.call(obj, name)
+               Reflect.has(obj, name)
             ) || objects[objects.length - 1];
         const descriptor = Object.getOwnPropertyDescriptor(target, name);
         if (descriptor?.set && descriptor?.get)

+ 29 - 0
tests/cypress/integration/scope.spec.js

@@ -56,3 +56,32 @@ test(
         get("span#two").should(haveText("foobar"));
     }
 );
+
+test(
+    "allows accessing class methods",
+    [
+        html`
+            <script>
+                class Counter {
+                    value = 0;
+                    constructor() {}
+                    increment() {
+                        this.value++;
+                    }
+                }
+                document.addEventListener("alpine:init", () => 
+                    Alpine.data("counter", () => new Counter())
+                )
+            </script>
+            <div x-data="counter">
+                <button type="button" @click="increment" x-text=value></button>
+            </div>
+        `,
+    ],
+    ({ get }) => {
+        get("button").should(haveText("0"));
+        get("button").click();
+        get("button").should(haveText("1"));
+    }
+);
+