fl00r
fl00r

Reputation: 83680

Clojurescript `:optimizations :advanced` breaks access to dataset

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)}

Upvotes: 2

Views: 181

Answers (2)

Alan Thompson
Alan Thompson

Reputation: 29974

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

Upvotes: 3

erdos
erdos

Reputation: 3538

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}

Upvotes: 1

Related Questions