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

Create implicit values for stores referenced with $ prefix anywhere in script #1889

Closed
Rich-Harris opened this issue Dec 20, 2018 · 2 comments · Fixed by #1941
Closed

Create implicit values for stores referenced with $ prefix anywhere in script #1889

Rich-Harris opened this issue Dec 20, 2018 · 2 comments · Fixed by #1941
Milestone

Comments

@Rich-Harris
Copy link
Member

Currently, referencing a reactive store foo as $foo works in templates and in reactive declarations. There's no real reason that shouldn't be extended to apply anywhere in the instance <script> block — especially since referencing $foo in an event handler already works if you've referenced $foo in one of the 'approved' ways.

@Rich-Harris Rich-Harris added this to the 3.x milestone Dec 20, 2018
@Conduitry
Copy link
Member

I'm assuming using $foo in one of these new places would just result in the current value of the store, without any sort of reactivity? I think we need to be careful about what happens if the module/instance-level foo is shadowed by a foo local to some method. What would $foo inside that method refer to then?

@Rich-Harris
Copy link
Member Author

Yeah — as an example, I'm reworking the REPL to use stores, and want to be able to do this sort of thing:

<!-- Repl.html -->
export function toJSON() {
  return {
    components: $components,
    values: $values
  };
}

Then the page can get the state of everything (to save it to a gist etc) with repl.toJSON(). It wouldn't make sense for that function to run reactively, it just needs to get a snapshot of those values. (We could achieve the same thing by adding a get() method to stores, or something, but then there'd be two different ways of referring to the same thing; I think this is preferable, and means that you don't need to learn special rules about where $foo is allowed.)

Since $foo is a real variable declared at the top level of the component, a reference to $foo wouldn't be shadowed by a reference to foo in some child scope. The reference isn't rewritten or manipulated in any way, it just causes the compiler to add the subscription code.

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 a pull request may close this issue.

2 participants