我是重新设计的新手,我想我忘记了一些明显的事情。我的网页正在运行,但当我单击单选按钮时似乎没有更新。基本上,我想要做的是,当单击单选按钮时,它会将我的value-name
atom 更新为 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)