diff --git a/site/content/docs/02-template-syntax.md b/site/content/docs/02-template-syntax.md index 070dc46687..11f59e3edc 100644 --- a/site/content/docs/02-template-syntax.md +++ b/site/content/docs/02-template-syntax.md @@ -545,6 +545,26 @@ Numeric input values are coerced; even though `input.value` is a string as far a ``` +##### A gotcha when using `on:` and `bind:` together + +Note that if you add an event handler and bind a property, they will be executed in the order you write them in the template. + +--- + +For example, here `onChange` will be attached (and called) before the internal event handler added by `bind:value`. + +```sv + +``` + +--- + +In the opposite case, if `bind:value` is before `on:change` then your change handler will see the updated bound value. + +```sv + +``` + ##### Binding ` + {#each items as item} + + {/each} + + +

This dropdown works as expected because bind:value is before on:change so that your change handler sees the updated bound value.

+ +

❗️ Event handler first

+ + +

This dropdown won't work (*) because by placing the on:change before the bind:value your event handler gets attached (and called) before the internal event handler added by bind:value.

+ +

(*) On Firefox both the dropdown and the selected value the won't update. On Chrome, the dropdown updates but the selected value won't.

+ +

Selected value

+ + + diff --git a/site/content/examples/05-bindings/12-binding-gotcha/meta.json b/site/content/examples/05-bindings/12-binding-gotcha/meta.json new file mode 100644 index 0000000000..fd0c67e609 --- /dev/null +++ b/site/content/examples/05-bindings/12-binding-gotcha/meta.json @@ -0,0 +1,3 @@ +{ + "title": "Binding gotcha" +} diff --git a/site/static/examples/thumbnails/binding-gotcha.jpg b/site/static/examples/thumbnails/binding-gotcha.jpg new file mode 100644 index 0000000000..415ac3d2a7 Binary files /dev/null and b/site/static/examples/thumbnails/binding-gotcha.jpg differ