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

$watch doesn't work with Array.prototype.push #383

Closed
FYITom opened this issue Apr 15, 2020 · 9 comments
Closed

$watch doesn't work with Array.prototype.push #383

FYITom opened this issue Apr 15, 2020 · 9 comments

Comments

@FYITom
Copy link

FYITom commented Apr 15, 2020

CodePen show's the issue, check the console.log when toggling filters.

https://codepen.io/TomS-/pen/NWGxdzv

@ryangjchandler
Copy link
Contributor

ryangjchandler commented Apr 15, 2020

Hi @FYITom, I'd probably use this.selected = this.selected.concat([item]) instead of this.selected.push(item). The behaviour of .push() and other Array.prototype is a bit temperamental with the reactivity core and the Proxy won't always react to changes and trigger watchers. Explicitely setting a property will definitely work.

I modified your CodePen here (https://codepen.io/ryangjchandler/pen/rNOxyLE?editors=1111) with the changes and the watcher is now reacting.

If you're okay with this, then please close the issue. If you want some further help, just ask, there's always someone here!

@HugoDF
Copy link
Contributor

HugoDF commented Apr 15, 2020

Is this a $watch issue? Isn't it a generic reactivity issue on array mutation methods?

@ryangjchandler
Copy link
Contributor

Is this a $watch issue? Isn't it a generic reactivity issue on array mutation methods?

Yeah exactly. I've had trouble with .push() before, without the $watcher.

@SimoTod
Copy link
Collaborator

SimoTod commented Apr 15, 2020

$watch triggers a function when the value changes (note that, when dealing with objects and arrays, the value is just a reference to the object so $watch only works if you reassign the variable as Ryan suggested).
At the moment, Alpine does not support 'deep' watchers (They would trigger when something inside the object or array changes at any level).
There is an initial PR for it #294 but it hasn't been reviewed yet.

@ryangjchandler
Copy link
Contributor

$watch triggers a function when the value changes (note that, when dealing with objects and arrays, the value is just a reference to the object so $watch only works if you reassign the variable as Ryan suggested).
At the moment, Alpine does not support 'deep' watchers (They would trigger when something inside the object or array changes at any level).
There is an initial PR for it #294 but it hasn't been reviewed yet.

Yeah exactly that. I think the PR could be tidied up a little before being reviewed / merged too.

@FYITom
Copy link
Author

FYITom commented Apr 15, 2020

Thank you very much for that, makes complete sense to me.

@FYITom FYITom closed this as completed Apr 15, 2020
@pohlt
Copy link
Contributor

pohlt commented Apr 27, 2020

I had the same issue with a Set. Adding or deleting an item does not trigger a watch or set/unset a class in an x-bind:class. After changing the set, I did a selected = new Set(selected) which triggers any updates.

@SimoTod
Copy link
Collaborator

SimoTod commented Apr 27, 2020

Deep watchers are not supported yet. The watcher triggers only if you reassign the variable but not if you change it's internal state.

@stuartjnelson
Copy link

I know this is old but this thread just got me out of a four hour hole-of-dev-doom.

Thanks @ryangjchandler for answering and @FYITom for asking (and both working off a Codepen demo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants