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>
Can you reproduce the issue?
Post the link using one of our bug report templates:
On which browser/OS does the issue appear?
All
How would you tag this issue?
@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!
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