Skip to content

Instantly share code, notes, and snippets.

@aamedina
Created September 25, 2013 21:42
Show Gist options
  • Select an option

  • Save aamedina/6706476 to your computer and use it in GitHub Desktop.

Select an option

Save aamedina/6706476 to your computer and use it in GitHub Desktop.
(deftemplate accounts-info
[accounts]
[:div#account-info
[:h1 "Remember this template...? It's back!"]
[:h2 [:strong "And better than ever!"]]
[:h2 "Accounts go here...!"]
(when (zero? (count accounts))
[:p "Aww, shucks. No accounts have been fetched yet!"])
(for [a (sort-by :name accounts)]
[:div.account-info
[:h2 (:name a)]
[:p (str a)]])])
(defn view-watcher
[f new-state selector]
(dommy/replace! (sel1 selector) (f new-state selector)))
(defn view
[resource-agent selector f]
(add-watch resource-agent selector
(fn [k ref old-state new-state]
(view-watcher f new-state selector)))
(let [dom-node (f @resource-agent selector)]
dom-node))
(defn account-by-name
[account-name]
(select #(= (clojure.string/lower-case (:name %)) (name account-name))
@(account)))
(defn remove!
[resource model]
(send resource difference (if (set? model) model #{model})))
(defn add!
[resource model]
(send resource union (if (set? model) model #{model})))
(defn update!
[resource new]
(let [old (select #(= (:id %) (:id new)) @resource)
-update (fn [xrel old new]
(-> (difference xrel old)
(union (when (set? new) new #{new}))))]
(send resource -update old new)))
(defn create!
[resource new]
(let [new (assoc new :id "-1")]
(add! resource #{(assoc new :id "-1")})
new))
(def accounts (account))
(def accounts-info-view (view accounts :#account-info accounts-info))
(dommy/append! (sel1 :#content) accounts-info-view)
(comment
(fetch! accounts)
(def acura (account-by-name "acura insider"))
(remove! accounts acura)
(add! accounts acura)
(def adrians (create! accounts {:name "Adrian's Campaign"}))
(def brendans (create! accounts {:name "Brendan's Campaign"}))
(remove! accounts brendans)
(remove! accounts adrians)
(add! accounts adrians))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment