2
votes

Following code:

(set! (.. e -target -dataset -some-field) "some-value")

is compiled into:

return a.target.dataset.Qh=Yf(b)

some_field is compressed into Qh. And I need it to be exactly some_field.

I understand this is due to compression optimization. But is there a way to hint, or to bypass this behavior?

PS: simple optimization gives the desired output

return a.target.dataset.some_field=cljs.core.name.call(null,b)}
2
Core team's recommendation seems to be to use goog.object/set for setting object fields; also you can add the dataset's structure to the externs file. However, both approaches seem overly verbose / complicated; I will be happy to know if there is a simpler recommended solution. - Aleph Aleph
In addition to defining an extern, you may need to use -some_field instead of -some-field. - exupero
@AlephAleph cool, I'll try with goog - fl00r
@exupero kebab-case will be compiled into snake-case I beleive - fl00r

2 Answers

3
votes

You may also be interested in the cljs-oops library: https://github.com/binaryage/cljs-oops

Then you can say:

(oset! el "innerHTML" "Hi!")

More examples below, and also on the CLJS Cheatsheet:

enter image description here

1
votes

The problem is that some-field (or is it some_field or someField?) gets simplified to Qh. This is because the compiler does not know that the dataset object has a some-field property.

  • One solution is to write extern files so that the Google Closure Compiler will know that the given field must not be renamed.

  • An other solution is to use aset function or call goog.object.set function. This way you reference the field of the object with a string value and the string values do not get simplified.

Second example:

cljs.user=> (def a (clj->js {"a" 1}))
#'cljs.user/a

cljs.user=> a
#js {:a 1}

cljs.user=> (aset a "b" 2)
2

cljs.user=> a
#js {:a 1, :b 2}