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 gets confused on nested properties #279

Closed
ahmedkandel opened this issue Mar 16, 2020 · 1 comment · Fixed by #292
Closed

$watch gets confused on nested properties #279

ahmedkandel opened this issue Mar 16, 2020 · 1 comment · Fixed by #292

Comments

@ahmedkandel
Copy link
Contributor

Sorry in advance, $watch has been just released and I was waiting for it. So I give it a try and found two points:

  1. When watching a property the callback gets fired regardless of the property parent e.g.
<div x-data="{ foo: 'bar', subObj: { foo: 'baz' } }" x-init="$watch('foo', value => console.log('$watch callback', value))">
    <button x-on:click="foo = 'qux'">Change 'foo' property</button>
    <button x-on:click="subObj.foo = 'quux'">Change 'subObj.foo' property</button>     
</div>

$watch callback gets fired on both cases, While should be more spesific.

  1. There is no deep watching like in Vue.js, Maybe it is a future feature, I don't want to stress you out e.g.
<div x-data="{ items: ['foo', 'bar'] }" x-init="$watch('items', value => console.log('$watch callback', value))">
    <ul>
        <template x-for="item in items" :key="item">
            <li x-text="item"></li>
        </template> 
    </ul>
    <button x-on:click="items.push('baz')">Add 'baz' to items</button>
</div>

$watch callback will not be fired when pushing to items array. The same thing if items is array of objects when changing a child object propery the callback is not fired.

Thanks.

@ahmedkandel
Copy link
Contributor Author

I am working on a draft PR that may fix those two issues. Hope it works.

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

Successfully merging a pull request may close this issue.

1 participant