Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns om-tut.core
  2.   (:require [om.core :as om :include-macros true]
  3.             [om.dom :as dom :include-macros true]))
  4.  
  5. (enable-console-print!)
  6.  
  7. (println "This text is printed from src/om-tut/core.cljs. Go AAAhead and edit it and see reloading in action.")
  8.  
  9. ;; define your app data so that it doesn't get over-written on reload
  10.  
  11. (defonce app-state
  12.   (atom
  13.     {:contacts
  14.      [{:first "Ben" :last "Bitdiddle" :email "benb@mit.edu"}
  15.       {:first "Alyssa" :middle-initial "P" :last "Hacker" :email "aphacker@mit.edu"}
  16.       {:first "Eva" :middle "Lu" :last "Ator" :email "eval@mit.edu"}
  17.       {:first "Louis" :last "Reasoner" :email "prolog@mit.edu"}
  18.       {:first "Cy" :middle-initial "D" :last "Effect" :email "bugs@mit.edu"}
  19.       {:first "Lem" :middle-initial "E" :last "Tweakit" :email "morebugs@mit.edu"}]}))
  20.  
  21. (defn middle-name [{:keys [middle middle-initial]}]
  22.   (cond
  23.     middle (str " " middle)
  24.     middle-initial (str " " middle-initial ".")))
  25.  
  26. (defn display-name [{:keys [first last] :as contact}]
  27.   (str last ", " first (middle-name contact)))
  28.  
  29. (defn contact-view [contact owner]
  30.   (reify
  31.     om/IRender
  32.     (render [this]
  33.       (dom/li nil (display-name contact)))))
  34.  
  35. (defn contacts-view [data owner]
  36.   (reify
  37.     om/IRender
  38.     (render [this]
  39.       (dom/div nil
  40.         (dom/h2 nil "Contact list")
  41.         (apply dom/ul nil
  42.           (om/build-all contact-view (:contacts data)))))))
  43.  
  44. (om/root contacts-view app-state
  45.   {:target (. js/document (getElementsById "contacts"))})
  46.  
  47. (defn on-js-reload []
  48.   ;; optionally touch your app-state to force rerendering depending on
  49.   ;; your application
  50.   ;; (swap! app-state update-in [:__figwheel_counter] inc)
  51. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement