我是新來重新框架,我想我忘了一些明顯的東西。我的網頁即將開始工作,但當我單擊單選按鈕時似乎沒有更新。基本上,我想要做的是單擊單選按鈕時,它會將我的value-name
原子更新爲2.這樣做會導致頁面刷新,因爲函數display-val
取決於value-name
。然而,沒有任何反應。網頁不更新
我感覺我應該訂閱一些東西,以便display-val
知道要更新。
這裏是我的代碼:
(def q-id (atom 0))
(def value-name (atom ""))
(def next-id (atom 0))
(defn create-answer
"Creates a radio button answer"
([question-id answer value name event next-question-id class description]
[:div {:class :control-container}
[:div {:class :control-left}
[:input {:type :radio :value value :name name :class class :data-next next-question-id :on-change (fn [e] (reset! value-name value) (reset! q-id question-id) (reset! next-id next-question-id) (re-frame/dispatch [event e]))}]]
[:div {:class :control-right} answer]
; If there is no description, then do not render the "control-right-description" div
(if-not (clojure.string/blank? description)
[:div {:class :control-right-description} description])
])
([question-id answer value name event next-question-id class]
(create-answer question-id answer value name event next-question-id class ""))
)
(defn display-val
[item]
(if (or (= @next-id (get item :id)) (and (get item :first) (= @next-id 0))) "block" "none")
)
(defn create-block
"Renders a question and the possible answers"
[item]
(if (get item :first)
[:div {:id (str "divQ" (get item :id)) :style {:display (display-val item)}}
[:div
[:p (get item :question)]
(for [answer (get item :answers)]
(create-answer (get item :id) (get answer :answer) (get answer :value) (get answer :name) (get answer :event) (get answer :nextQuestionId) (get answer :className) (get answer :description))
)
]
]
[:div {:id (str "divQ" (get item :id)) :style {:display (display-val item)}}
[:div
[:p (get item :question)]
(for [answer (get item :answers)]
(create-answer (get item :id) (get answer :answer) (get answer :value) (get answer :name) (get answer :event) (get answer :nextQuestionId) (get answer :className) (get answer :description))
)
]
]
)
)
(defn render-questions
"Renders the questions and answers, hiding all but the first question and answer set"
[]
(for [item cfg/questions]
(create-block item)
)
)
(defn main-panel []
(let [name (re-frame/subscribe [:name])]
(fn []
[:div
; Render the questions
(render-questions)
]
)))
(defn handle-buttons-part-one
[db e]
;(js/alert @value-name)
)
(re-frame/reg-event-db :buttons-part-one handle-buttons-part-one)
哪裏是'CFG/questions'? – akond