Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to ClojureScript Conversion

I am trying to convert the following line to Cljs,

var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy:  true });

What would be the corresponding map for,

{ enableHighAccuracy:  true }

in ClojureScript? Is enableHighAccuracy a symbol?

like image 989
Hamza Yerlikaya Avatar asked Jul 10 '26 18:07

Hamza Yerlikaya


1 Answers

If you're using interop with a library (such as geolocation, in your case), and it expects a JavaScript object, you'll need to pass it a standard JavaScript object, not a Clojure map.

You can construct one of these in ClojureScript by using the js-obj function with no arguments. This will return an empty JavaScript with no fields; you can then add fields using the aset function.

So your code would be something like:

(let [arg (js-obj)]
  (aset arg "enableHighAccuracy" true)
  (.watchPosition (.geolocation navigator) on-success on-error arg))

If this is something you find yourself doing a lot, it's easy to write a utility function to convert a ClojureScript map into a JavaScript object using whatever defaults you like; an example of such a function can be found in this gist; https://gist.github.com/1658431

like image 67
levand Avatar answered Jul 13 '26 10:07

levand