-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Added check that the ${each_block_value} exists #682
Conversation
Thanks for the PR. It depends on the behaviour we want — elsewhere, Svelte errs on the side of demanding explicitness rather than trying to guess what the developer meant, because that way it's easier to track down errors. For example I'm a Brit on an American team, which means misunderstandings like this aren't all that uncommon: {{#each colours as colour}}
<span style='color: {{colour}};'>{{colour}}</span>
{{/each}}
<script>
export default {
data () {
return {
colors: ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
};
}
};
</script> Right now, that gives me an error — the proposal in #681 was to provide a more useful error if the component was compiled with Incidentally, caching |
I just observed that when using the Proxy to wrap an object the length was called every time. |
At least I was able to create a failing test :) |
I think that's just the Heisenberg effect — the property is accessed because there's a proxy |
How should we handle such situation? Uninitialized fields should
|
I belived this was an easy fix :) I'm obviously not smart enough to fix it :( |
Nonsense! It just failed because of a snapshot test. It's the correct fix if we want to prevent errors regardless of whether there's data for the each block. I'm inclined to argue against doing that for the reasons mentioned above, but I'm open to persuasion! If anyone else out there has thoughts on the matter, let us know. |
I'd vote for not preventing the error. It's more in line with how other things in Svelte work, and it might help you catch typos or other bugs. With the other recent change (#683) it might not even be necessary to add a specific dev warning about this, but I feel less strongly about that. |
I belive there are good reasons for having to "predefine" all fields/properties on your viewmodel. Maybe throw an exception with a good description about witch field is missing. What if the field is there, but is null. Is this the same situation? Should the check be against the hasOwnProperty and don't evaluate the value? |
Template: If data looks like this it will throw exception |
Should we keep this PR open @Rich-Harris or should I delete it? Did we end up with a conclusion? |
I think we should probably stick with the current behaviour — masking user errors is almost always a bad idea. If you compile with (This is the REPL running locally, it doesn't actually use So I'll close this PR. Thank you anyway! |
Also chaches the length of the array in a variable to avoid quering for it every time.
Does this fix #681 ?