Riot: Suggestion: To adopt explicit context injection

Created on 29 Oct 2020  路  3Comments  路  Source: riot/riot

Help us to manage our issues by answering the following:

  1. Describe your issue:

I think the "Options API Syntax" of Riot@4/5 like Vue@2 is mystifying, and I've been writing the component files as below:
(refs: https://github.com/voorhoede/riotjs-style-guide#assign-this-to-tag , https://github.com/voorhoede/riotjs-style-guide#put-tag-properties-and-methods-on-top )

  <script>
    import User from '../../includes/user/user.riot'
    SideBar.components = {
      User
    }
    export default SideBar

    function SideBar() {
      const tag = {}
      tag.state = {
        name: 'John',
        showUser: false
      }
      tag.toggleUser = toggleUser

      return tag

      function toggleUser() {
        this.update({
          showUser: !this.state.showUser
        })
      }
    }
  </script>

Now, in function toggleUser(), there is this as context. But I don't want to use the ambiguous word.
How about to adopt explicit context object injection?

ex)

<script>
export default SideBar
// Inject a context object as parameter of Riot component factory.
function SideBar(ctx) {
  // the context object is used from inner scope.
  function toggleUser() {
    ctx.update({
      showUser: !ctx.state.showUser
    })
  }
}
</script>
  1. Can you reproduce the issue?

    Post the link using one of our bug report templates:

  2. On which browser/OS does the issue appear?

All

  1. Which version of Riot does it affect?

Riot@4/5

  1. How would you tag this issue?

    • [ ] Question
    • [ ] Bug
    • [x] Discussion
    • [x] Feature request
    • [ ] Tip
    • [ ] Enhancement
    • [ ] Performance
answered question

Most helpful comment

@ShiMeiWo the feature you want to achieve can be done with 2 lines of javascript. Please check this example where I am using a decorator pattern to get the context in a functional way https://plnkr.co/edit/MWw4EcXgG70QHDqQ

Of course you can use also a Riot.js plugin for it but I prefer keeping these behaviors explicit

All 3 comments

@GianlucaGuarini You've added [question] label. Do you need more informations about this issue? I am not good at English, and using "DeepL Translator". If you don't make some senses, I'll try to translate again and re-post about the issue.

@ShiMeiWo the feature you want to achieve can be done with 2 lines of javascript. Please check this example where I am using a decorator pattern to get the context in a functional way https://plnkr.co/edit/MWw4EcXgG70QHDqQ

Of course you can use also a Riot.js plugin for it but I prefer keeping these behaviors explicit

Thanks a lot, @GianlucaGuarini !
The idea makes my day!

Was this page helpful?
0 / 5 - 0 ratings