clojure - Clojurescript/OM persist app-state through refresh -


i followed basic tutorial on om's github basic tutorial simple form enables array of contacts stored within app state modified however, these modifications 'reset' upon page refresh.

if remove 3 people current app-state , refresh, these 3 people present in app-state again.

the code same tutorial, ill post anyway:

(ns ^:figwheel-always om-tut.core   (:require-macros [cljs.core.async.macros :refer [go]])   (:require [om.core :as om :include-macros true]             [om.dom :as dom :include-macros true]             [cljs.core.async :refer [put! chan <!]]             [clojure.data :as data]             [clojure.string :as string]))  (enable-console-print!)  (println "edits text should show in developer console.")  ;; define app data doesn't over-written on reload  (defonce app-state   (atom     {:contacts      [{:first "ben" :last "bitdiddle" :email "benb@mit.edu"}       {:first "alyssa" :middle-initial "p" :last "hacker" :email "aphacker@mit.edu"}       {:first "eva" :middle "lu" :last "ator" :email "eval@mit.edu"}       {:first "louis" :last "reasoner" :email "prolog@mit.edu"}       {:first "cy" :middle-initial "d" :last "effect" :email "bugs@mit.edu"}       {:first "lem" :middle-initial "e" :last "tweakit" :email "morebugs@mit.edu"}]}))  (defn middle-name [{:keys [middle middle-initial]}]   (cond     middle (str " " middle)     middle-initial (str " " middle-initial ".")))  (defn display-name [{:keys [first last] :as contact}]   (str last ", " first (middle-name contact)))  (defn contact-view [contact owner]   (reify     om/irenderstate     (render-state [this {:keys [delete]}]       (dom/li nil         (dom/span nil (display-name contact))         (dom/button #js {:onclick (fn [e] (put! delete @contact))} "delete")))))  (defn handle-change [e owner {:keys [text]}]    (let [value (.. e -target -value)]      (if-not (re-find #"[0-9]" value)        (om/set-state! owner :text value)        (om/set-state! owner :text text))))   (defn parse-contact [contact-str]   (let [[first middle last :as parts] (string/split contact-str #"\s+")         [first last middle] (if (nil? last) [first middle] [first last middle])         middle (when middle (string/replace middle "." ""))         c (if middle (count middle) 0)]     (when (>= (count parts) 2)       (cond-> {:first first :last last}         (== c 1) (assoc :middle-initial middle)         (>= c 2) (assoc :middle middle)))))  (defn add-contact [data owner]   (let [new-contact (-> (om/get-node owner "new-contact")                         .-value                         parse-contact)]     (when new-contact       (om/transact! data :contacts #(conj % new-contact))       (om/set-state! owner :text ""))))  (defn contacts-view [data owner]   (reify     om/iinitstate     (init-state [_]       {:delete (chan)        :text ""})     om/iwillmount     (will-mount [_]       (let [delete (om/get-state owner :delete)]         (go (loop []               (let [contact (<! delete)]                 (om/transact! data :contacts                   (fn [xs] (vec (remove #(= contact %) xs))))                 (recur))))))     om/irenderstate     (render-state [this state]       (dom/div nil         (dom/h2 nil "contact list")         (apply dom/ul nil           (om/build-all contact-view (:contacts data)             {:init-state state}))         (dom/div nil           (dom/input             #js {:type "text" :ref "new-contact" :value (:text state)                  :onchange #(handle-change % owner state)})           (dom/button #js {:onclick #(add-contact data owner)} "add contact"))))))   (om/root contacts-view app-state   {:target (. js/document (getelementbyid "contacts"))})   (defn on-js-reload []   ;; optionally touch app-state force rerendering depending on   ;; application   ;; (swap! app-state update-in [:__figwheel_counter] inc) ) 

data on client has either persisted on server or in local storage. refreshing browser/reloading browser same starting scratch. so, data had gone, unless persisted it.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -