Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@johnbintz
Created April 18, 2014 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbintz/11018653 to your computer and use it in GitHub Desktop.
Save johnbintz/11018653 to your computer and use it in GitHub Desktop.
clj->into->js
(defn- map-names [items]
(into {} (mapv (fn [item] {(item :name) item}) items))
)
(defn- reset-objects! [objects mapped-clj-objects]
(.forEach objects
(fn [object]
; the objects in these happen to be indexed by object.name
(let [clj-object (mapped-clj-objects (.-name object))]
(doseq [[k v] clj-object]
; this happens during a $digest, so the data won't update until the $digest is done
(aset object (name k) (cond
; some of the data is stored in keywords, 'cause Clojure
(keyword? v) (name v)
; the rest can come through just fine
:else v
))
)
)
))
)
(defn clj->into->js [clj js]
(let [array-mappings [:array-one :array-two]
without-arrays (apply dissoc clj array-mappings)
without-arrays-js (clj->js without-arrays)
]
(doseq [mapping array-mappings]
(let [mapped (map-names (clj mapping))
js-name (name mapping)
js-objs (aget js js-name)]
(reset-objects! js-objs mapped)
(aset without-arrays-js js-name js-objs)
)
)
without-arrays-js
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment