Created
April 18, 2014 00:22
-
-
Save johnbintz/11018653 to your computer and use it in GitHub Desktop.
clj->into->js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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