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

Fix $watch scope, Support nested properties, Support watch deep #294

Closed
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,22 @@ You can also use `$dispatch()` to trigger data updates for `x-model` bindings. F

You can "watch" a component property with the `$watch` magic method. In the above example, when the button is clicked and `open` is changed, the provided callback will fire and `console.log` the new value.

Use dot-delimited paths to watch properties of nested objects like `foo.bar.baz`. For example:

```html
<div x-data="{ foo: { bar: { baz: false } } }" x-init="$watch('foo.bar.baz', value => console.log(value))">
<button @click="foo.bar.baz = ! foo.bar.baz">Toggle Nested Baz</button>
</div>
```

You can also detect nested value changes inside objects "deep watching", By passing `{ deep: true }` as the options argument. For example:

```html
<div x-data="{ items: { foo: 'bar', baz: [ 1, 2, 3 ] } }" x-init="$watch('items', value => console.log(value), { deep: true })">
<button @click="items.baz.push(4)">Push To Baz</button>
</div>
```

## v3 Roadmap
* Move from `x-ref` to `ref` for Vue parity

Expand Down
Loading