How does one deal with dashes in variable names in...
# javascript
g
How does one deal with dashes in variable names in KJS?
Copy code
`grapesjs-lory-slider` = jsObject<dynamic> {
"name contains illegal identifiers that can't appear in javascript identifiers" In JS it works like this:
Copy code
'grapesjs-lory-slider': {
            sliderBlock: {
              category: 'Extra'
            }
b
Copy code
`'my-dashed-name'`
Anything in backtics is translated to js as-is
I think*. Give it a go and let us know if it works
g
Was my first guess, nope. This seems to be a BIG bug in KJS or the IDEA plugin maybe.
This is the kind of stuff that makes me question everything... if it's even a good idea to do anything except raw javascript... basic stuff can't be done, KJS hasn't been made a comprehensive or cohesive system, feels like its being designed as they go along... probably okay for apps but not entire languages. Why am I doing this to myself, lol.
MapOf()<string, dynamic> actually compiles... but then I see mapOf() in the JS code not a json map and the intended effects are not observed... pretty sure it has to be a jsObject to get the json output needed but that silly function doesn't know about the `
b
What about
Copy code
Val jsObj = jsObject().asDynamic();
jsObj['dashed-key'] = 1;
Or even
Copy code
global.asDynamic()['dashed-key']=1;
g
I just solved it thanks to this post. https://kotlinlang.slack.com/archives/C0B8L3U69/p1597445606499200
Copy code
pluginsOpts = jsObject<dynamic> {
    this["grapesjs-lory-slider"] = jsObject<dynamic> {
        sliderBlock = jsObject<dynamic> {
            category = "Extra"
        }
    }
}
b
Yep, same pattern. Good stuff
g
Yeah... I think I tried this... but I think I was trying this. instead of this[]...
thinking python I guess... never used this much in kotlin... more it, lol
Thanks man
b
Np, glad to have learned it myself 😀
But still, I don't see why the compiler can't do it for us with standard ` notation
Clearly it's not a platform limitation
g
Agreed, I started an issue ticket, hope they realize... kinda hope jsObject is brought to a higher level too... I mean... it's all type safe great, but my JSON is very verbose in kotlin compared to JS... and not sure I really learned what advantage it provides.
still... I mean... this[""] caused me to waste a day. 8 characters cost my client like $5k, lol
Another Tip for anyone searching the forum: Dynamics have to be dynamic all the way down for the 'this' to work right, and it may compile without issue. So many things at play here, I assumed type would be inherented from parent jsObject if it was dynamic and not specified... wrong.
Copy code
pluginsOpts = jsObject<dynamic> {
    this["grapesjs-lory-slider"] = jsObject<dynamic> {
        sliderBlock = jsObject<dynamic> {
            category = "Extra"
        }
    }
    this["grapesjs-tabs"] = jsObject<dynamic> {
        tabsBlock = jsObject<dynamic> {
            category = "Extra"
        }
    }
    this["grapesjs-typed"] = jsObject<dynamic> {
        block = jsObject<dynamic> {
            category = "Extra"
            content = jsObject<dynamic>  {
                type = "typed"
                this["type-speed"] = 40
                strings = arrayOf<String>(
                    "Text row one",
                    "Text row two",
                    "Text row three"
                )
            }
        }
    }
}
Works
b
That's why I love kotlin community. Even after the issue is resolved, people still come back to leave breadcrumbs for other who might face the same issue
😎 1