Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixes issue with setters accessing deeply nested data #4265

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/alpinejs/src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ let mergeProxyTrap = {
) || objects[objects.length - 1];
const descriptor = Object.getOwnPropertyDescriptor(target, name);
if (descriptor?.set && descriptor?.get)
return Reflect.set(target, name, value, thisProxy);
// Can't use Reflect.set here due to [upstream bug](https://github.com/vuejs/core/blob/31abdc8adad569d83b476c340e678c4daa901545/packages/reactivity/src/baseHandlers.ts#L148) in @vue/reactivity
return descriptor.set.call(thisProxy, value) || true;
return Reflect.set(target, name, value);
},
}
Expand Down
48 changes: 48 additions & 0 deletions tests/cypress/integration/scope.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,51 @@ test(
get("button").should(haveText("clicked"));
}
);

test(
"properly merges the datastack with nested data",
[
html`
<div x-data="{ foo: { bar: 'fizz' } }">
<div x-data="{ bar: 'buzz' }">
<span
id="1"
x-text="foo.bar + bar"
@click="foo.bar = foo.bar + bar"
></span>
</div>
<span id="2" x-text="foo.bar"></span>
</div>
`,
],
({ get }) => {
get("span#1").should(haveText("fizzbuzz"));
get("span#2").should(haveText("fizz"));
get("span#1").click();
get("span#1").should(haveText("fizzbuzzbuzz"));
get("span#2").should(haveText("fizzbuzz"));
}
);

test(
"handles getter setter pairs of object",
[
html`
<div x-data="{ foo: { bar: 'fizzbuzz' } }">
<div
x-data="{ get bar() { return this.foo.bar }, set bar(value) { this.foo.bar = value } }"
>
<span id="one" x-text="bar" @click="bar = 'foobar'"></span>
</div>
<span id="two" x-text="foo.bar"></span>
</div>
`,
],
({ get }) => {
get("span#one").should(haveText("fizzbuzz"));
get("span#two").should(haveText("fizzbuzz"));
get("span#one").click();
get("span#one").should(haveText("foobar"));
get("span#two").should(haveText("foobar"));
}
);
Loading