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

Syntax support #6610

Closed
tanekim88 opened this issue Aug 2, 2021 · 2 comments
Closed

Syntax support #6610

tanekim88 opened this issue Aug 2, 2021 · 2 comments

Comments

@tanekim88
Copy link

tanekim88 commented Aug 2, 2021

Describe the problem

<script>
 const obj = getObject();
// obj can be { items: [1,2,3]}; or { items: undefined};
 const lookup = {
1: {
valid: true,
value:'good'
},
2:{
valid: true,
value:'bad'
},
3:{
valid: false,
value:'soso'
}
}
</script>
{#if obj.items}
     {#each obj.items as item}
// Here, I wish I could just do  {#each obj?.items as item}
// Instead of having to wrap with if block to
// check if obj.items is not undefined.
// Currently, it errors out if obj.items is null without the #if block

           {#if lookup[item].valid}
// Here I want to declare a variable:
// const lookupItem = lookup[item]; so that I can use 
//lookupItem.value instead of lookup[item].value in the body.
// Is there way to do so?
// I wish I could declare it when using if block
// {#if lookup[item] as lookupItem}
// or it would be also good if I could use local block like
// {# const lookupItem = lookup[item]; /}
                 <span>lookup[item].value</span>
           {/if}
     {/each}
{#if}

Currently, my attempt to use Svelte for my current project looks something like this (note that the complicated Array.from(...) equations could be avoided if there was a way to declare a variable inside the parent's each block and do calculations to get range value and store inside the variable.):
image

In Angular, there is a way to declare a variable through the if block,
https://angular.io/api/common/NgIf
image

I wish svelte could provide a solution like this.

Describe the proposed solution

I already described in the comments

Alternatives considered

I already described in the comments

Importance

would make my life easier

@bluwy
Copy link
Member

bluwy commented Aug 2, 2021

Looks like you're requesting two features:

  1. You want Svelte to auto-handle falsy values when itertaing with {#each}. Duplicate of Support falsy values being passed to #each to avoid an incidental conditional #1791
  2. You want declare a variable inline in markup. Pending PR [feat] Implement RFC 33 - Constants in markup #6413

@tanekim88
Copy link
Author

@bluwy Thanks! I will close this for now

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