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 for(key in map) helper for stache #671

Open
mikemitchel opened this issue Feb 13, 2019 · 2 comments
Open

Create for(key in map) helper for stache #671

mikemitchel opened this issue Feb 13, 2019 · 2 comments

Comments

@mikemitchel
Copy link
Contributor

It would helpful to be able to iterate through keys of a Map in stache, and have stache do the DOM diffing if a key gets added, removed, or the key's value changes. The order of the keys inserted to the underlying Map(s) would need to be preserved to work in this use case, a Message Log.

For example, I have traceLog Map --> pageName Map--> message Map --> fragments List

data:

traceLog: { // map
  pageName: { // map
    messageKey: { // map
      fragments: [ // list
        {format: 'var', msg: 'first name'},
        {msg: ' = '},
        {format: 'val', msg: 'Max'}
      ]
    }
  }
}

stache:

{{# for(page in traceLog) }} -> map iteration
    render page stuff
  {{# for(message in page) }} -> map iteration
       render message stuff
     {{# for(fragment of message.fragments) }} -> list iteration
        render fragment stuff
      {{/for}}
  {{/for}}
{{/for}}

this would likely involve some changes in the way can-view-live handles patches as per discussion with @justinbmeyer

@matthewp
Copy link
Contributor

What's the problem with for/of exactly?

@mikemitchel
Copy link
Contributor Author

mikemitchel commented Feb 13, 2019

I'm looking for a few things here @matthewp does for/of do all these?:

  • underlying Map retains order of inserted keys so iteration order can be assured in stache (this is likely ok already, but not guaranteed like an ES6 Map)
  • if a key is added/removed from the Map, or the value for that key is updated, stache should only re-render that diff
  • access to the current key for that Map, so for (page in traceLog) above would allow me to do {{page}} in the stache. I guess you would have to do {{let value = traceLog[page]}} then to use the value, unless the helper worked like {{for (pageKey, pageValue of traceLog)}} to capture both the key and value for use in the stache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants