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 binding for each block local variable #4861

Merged

Conversation

tanhauhau
Copy link
Member

@tanhauhau tanhauhau commented May 19, 2020

Fixes: #4757
Fixes #4517

Changes:

  • remove the [bindingGroup].forEach(group => { loop to just use bindingGroup.
  • when binding within a {#each}, always replace the lhs object to each_value[each_index] form. and also remove the array variable from the bind callback. ie:
<script>
    let a = [1,2,3]
</script>

{#each a as a}
    <input bind:value={a}>
{/each}
// from

let a = [1, 2, 3];
function input_input_handler(a, each_value, a_index) {
  // `a` refers to the item instead of the array
  a.foo = this.value;
  $$invalidate(0, a);
}

// to

let a = [1, 2, 3];
function input_input_handler(each_value, a_index) { // removed `a` from params when necessary
  // uses each_value[a_index] instead of `a`
  each_value[a_index].foo = this.value;

  // a refers to the array, rather than the item.
  $$invalidate(0, a);
}

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR relates to an outstanding issue, so please reference it in your PR, or create an explanatory one for discussion. In many cases features are absent for a reason.
  • This message body should clearly illustrate what problems it solves. If there are related issues, remember to reference them.
  • Ideally, include a test that fails without this PR but passes with it. PRs will only be merged once they pass CI. (Remember to npm run lint!)

Tests

  • Run the tests tests with npm test or yarn test)

@tanhauhau tanhauhau force-pushed the tanhauhau/each-block-scope-shadow-bind branch 4 times, most recently from e9dbd4f to cdbceca Compare May 20, 2020 06:38
@tanhauhau tanhauhau force-pushed the tanhauhau/each-block-scope-shadow-bind branch from cdbceca to a29b185 Compare May 20, 2020 07:16
@tanhauhau tanhauhau mentioned this pull request May 20, 2020
4 tasks
@tanhauhau tanhauhau added the each label May 31, 2020
@tanhauhau tanhauhau mentioned this pull request Jun 1, 2020
4 tasks
@Conduitry Conduitry merged commit bf6c74f into sveltejs:master Jun 8, 2020
@tanhauhau tanhauhau deleted the tanhauhau/each-block-scope-shadow-bind branch June 9, 2020 00:40
taylorzane pushed a commit to taylorzane/svelte that referenced this pull request Dec 17, 2020
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 this pull request may close these issues.

Name collision in each disables binding bind:this to each value compiles into undefined variable names
2 participants