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

Why doesn't {{#with}} set context when it's nested? #3408

Open
ceremcem opened this issue May 15, 2022 · 3 comments
Open

Why doesn't {{#with}} set context when it's nested? #3408

ceremcem opened this issue May 15, 2022 · 3 comments

Comments

@ceremcem
Copy link

Description:

I remember (but I'm unsure) that we could use {{#with foo}} to set the context to foo. It still works the same way, but not with nested structures. Why is that?

Versions affected:

1.4

Platforms affected:

Browser

Reproduction:

Ractive({
  el: 'body',
  append: true,
  data: {
    foo: {
    }
  },
  template: `
    <!-- context becomes foo -->
    {{#with foo as f}}
      <input value="{{f.hello}}" />
      {{#with f.bar as b}}
      	<input value="{{b.yay}}" />
      {{/with}}
    {{/with}}
  `,
  onrender: function(){
    console.log(JSON.stringify(this.get()))
  }
})

This code prints the expected output: {"foo":{"bar":{"yay":""},"hello":""}}

However, this simplified version produces an undesired result:

Ractive({
  el: 'body',
  append: true,
  data: {
    foo: {
    }
  },
  template: `
    <!-- context becomes foo -->
    {{#with foo}}
      <input value="{{hello}}" />
      {{#with bar}}
      	<input value="{{yay}}" />
      {{/with}}
    {{/with}}
  `,
  onrender: function(){
    console.log(JSON.stringify(this.get()))
  }
})```

Outputs: `{"foo":{"hello":""}}`

Is this behavior expected?
@evs-chris
Copy link
Contributor

This is correct according to the tests, even though I agree that it doesn't look right and I've fought with it before. The problem is overloading and historical implementation detail in that originally with was just a plain mustache that's an internal if-with under the covers. To avoid the issue, foo.bar has to be in place in order to render the {{#with bar}} section, which will cause the yay twoway binding to update the model with the empty string.

@ceremcem
Copy link
Author

Do you think this desired behavior may be a feature request?

@evs-chris
Copy link
Contributor

I've definitely got it on my list for Ractive 2.0.

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

2 participants