2016-12-19 10 views
0

Clojureはすべて不変のデータに関するものです。だから、もしあなたが原子を持っているなら(値0で)、その値を変更する方法は、原子の参照を外してスワップを使うことです!またはリセット! (値1で今言う)。しかし、全体的に原子はまだ値0を持ち、特定のリセット状態にあります!またはスワップ!私はこれが正しいと信じています。私の質問は、私はもっとはっきりClojure - 異なる関数内の原子の値を変更する

(def table-data {:headers ["RIC-Code" "Isin" "Currency" "Description"] 
       :rows []}) 

(defonce fields (atom {})) 
(defonce errors (atom nil)) 
(defonce isin (atom "test") 
(defonce tdata (atom table-data)) 


(defn get-ric [ric isin] 
    (GET "/search-isin" 
     {:params {:ric ric} 
     :headers {"Accept" "application/transit+json"} 
     :handler #(do 
        (.log js/console (str "get-msg response : " %)) 
        (reset! isin %) 
        (reset! tdata {:headers ["RIC-Code" "Isin" "Currency" "Description"] 
            :rows @isin}))})) 



(defn replace-value [struct] 
    (walk/prewalk-replace {"id" "name"} struct)) 
(defn isin-form2 [isin] 
    [:div.content 
    [errors-component errors :server-error] 
    [:div.form-group 
    [errors-component errors :name] 
    [:p "Enter RIC-Code:" 
    [:input.form-control 
     {:type  :text 
     :name  :ric 
     :on-change #(swap! fields assoc :ric (-> % .-target .-value)) 
     :value  (:ric @fields)}]] 
    [errors-component errors :message] 
    [:input.btn.btn-primary 
    {:type  :submit 
     :on-click #(do 
        (get-ric (:ric @fields) isin)) 
     :value "Search RIC-Code"}] 
    [rt/reagent-table tdata] 
    [:input.btn.btn-primary 
    {:type  :submit 
     :on-click #(do 
        (reset! tdata {:headers ["RIC-Code" "Isin" "Currency" "Description"] 
           :rows (swap! isin (replace-value %))})) 
     :value "Change"}]]]) 

上記のコードはありません、ISIN-Form2の中で、それは最初のかかりは何1

にあるアトムの値を変更し、別の機能を持たせることができます値を設定し、キーワードricでフィールドに設定します。最初のボタンはget-ric関数を呼び出し、tdataを返します。 (get-ricでisinはベクトルのベクトルである原子です。[["id" ...] [...] ... [...]])2番目のボタンに何をしたいのですか? replace-valueを使用して、nameの値idをスワップします。しかし、私が交換しようとすると、2番目のボタンで!その値は変更されていません

答えて

0

おそらくこれは、それがどのように動作するかを説明します:

(def my-state (atom 0)) 

(defn fn1 [] 
    (newline) 
    (println :fn1-enter @my-state) 
    (swap! my-state inc) 
    (println :fn1-exit @my-state) 
    (newline)) 

(defn fn2 [] 
    (newline) 
    (println :fn2-enter @my-state) 
    (swap! my-state inc) 
    (println :fn2-exit @my-state) 
    (newline)) 

(println :main-1 @my-state) 
(fn1) 
(println :main-2 @my-state) 
(fn2) 
(println :main-3 @my-state) 

結果と

:main-1 0 

:fn1-enter 0 
:fn1-exit 1 

:main-2 1 

:fn2-enter 1 
:fn2-exit 2 

:main-3 2 
0

swap!には機能がありません。試してみてください:

(swap! isin #(replace-value %))

関連する問題