如何使用 Reagent Hiccup 添加点击事件以在 ClojureScript 中触发 JavaScript 事件
How do I add a click event to trigger a JavaScript event in ClojureScript using Reagent Hiccup
我刚开始使用 Reagent,对 Clojure 还很陌生。
我创建了一个移动菜单功能,并希望移动汉堡菜单可以点击以显示实际菜单。再次点击时,菜单必须隐藏。我不知道该怎么做
(defn mobile-menu [primary-menu secondary-menu logo-el]
[:div#ca-horizontal-mobile
[:div#logo logo-el]
[:div#menu-button
[:a
[:i.icon.bars]]]
[:header#menu.mobile
[:div
[:div.center.menu
(let [menu (concat primary-menu secondary-menu)]
(for [i menu]
[:div.item {:key (:key i)}
[:a.item {:id (:key i)
:href (:target i)} (:name i)]]))
[:div.item
[:a
{:style {:cursor :pointer}
:id :logout
:on-click #(re-frame/dispatch [:logout])} (str "Logout")]]]]]])])
主播需要展开和隐藏菜单
[:a
[:i.icon.bars]]]
我只需要一个示例,说明如何对 JavaScript 事件进行 JavaScript 调用,以及一点帮助理解它。
我在网上找到了这个片段 https://www.reddit.com/r/Clojure/comments/4ofct5/calling_methods_on_an_element_in_a_reagent/ 但不确定它是如何连接到任何东西上的。 .play
元素如何知道要做什么 on-click
?
(defn video-elem [src-url uid]
[:div
[:video {:src src-url :id uid}]
[:button {:on-click #(.play (.getElementById js/document uid))} "Play!"]
[:button {:on-click #(.pause (.getElementById js/document uid))} "Pause!"]])
在同事的帮助下,我们添加了以下内容。不过我不确定我是否解释正确。
(re-frame/reg-event-db
:toggle-mobile-menu
(fn [db _]
(assoc db :mobile-menu-visible (not (:mobile-menu-visible db)))))
(re-frame/reg-sub :show-mobile-menu #(:mobile-menu-visible %))))
(defn mobile-menu [primary-menu secondary-menu logo-el]
[:div#ca-horizontal-mobile
[:div#logo logo-el]
[:div#menu-button
{:on-click #(re-frame/dispatch [:toggle-mobile-menu])}
[:i.icon.bars]]
(let [show-menu (re-frame/subscribe [:show-mobile-menu])]
(if @show-menu
[:header#menu.mobile
[:div
[:div.center.menu
(let [menu (concat primary-menu secondary-menu)]
(for [i menu]
[:div.item {:key (:key i)}
[:a.item {:id (:key i)
:href (:target i)} (:name i)]]))
[:div.item
[:a
{:style {:cursor :pointer}
:id :logout
:on-click #(re-frame/dispatch [:logout])} (str "Logout")]]]]]))])]])
菜单按钮单击事件调度 :toggle-mobile-menu
re-frame/reg-event-db 切换菜单的可见性
show_menu
绑定订阅re-frame/reg-sub:show-menu-mobilewhich gets the
:mobile-menu -来自 db
的可见值
数据库中的 @show-menu
值被解构为一个真值,指示菜单应该隐藏还是显示。
我刚开始使用 Reagent,对 Clojure 还很陌生。
我创建了一个移动菜单功能,并希望移动汉堡菜单可以点击以显示实际菜单。再次点击时,菜单必须隐藏。我不知道该怎么做
(defn mobile-menu [primary-menu secondary-menu logo-el]
[:div#ca-horizontal-mobile
[:div#logo logo-el]
[:div#menu-button
[:a
[:i.icon.bars]]]
[:header#menu.mobile
[:div
[:div.center.menu
(let [menu (concat primary-menu secondary-menu)]
(for [i menu]
[:div.item {:key (:key i)}
[:a.item {:id (:key i)
:href (:target i)} (:name i)]]))
[:div.item
[:a
{:style {:cursor :pointer}
:id :logout
:on-click #(re-frame/dispatch [:logout])} (str "Logout")]]]]]])])
主播需要展开和隐藏菜单
[:a
[:i.icon.bars]]]
我只需要一个示例,说明如何对 JavaScript 事件进行 JavaScript 调用,以及一点帮助理解它。
我在网上找到了这个片段 https://www.reddit.com/r/Clojure/comments/4ofct5/calling_methods_on_an_element_in_a_reagent/ 但不确定它是如何连接到任何东西上的。 .play
元素如何知道要做什么 on-click
?
(defn video-elem [src-url uid]
[:div
[:video {:src src-url :id uid}]
[:button {:on-click #(.play (.getElementById js/document uid))} "Play!"]
[:button {:on-click #(.pause (.getElementById js/document uid))} "Pause!"]])
在同事的帮助下,我们添加了以下内容。不过我不确定我是否解释正确。
(re-frame/reg-event-db
:toggle-mobile-menu
(fn [db _]
(assoc db :mobile-menu-visible (not (:mobile-menu-visible db)))))
(re-frame/reg-sub :show-mobile-menu #(:mobile-menu-visible %))))
(defn mobile-menu [primary-menu secondary-menu logo-el]
[:div#ca-horizontal-mobile
[:div#logo logo-el]
[:div#menu-button
{:on-click #(re-frame/dispatch [:toggle-mobile-menu])}
[:i.icon.bars]]
(let [show-menu (re-frame/subscribe [:show-mobile-menu])]
(if @show-menu
[:header#menu.mobile
[:div
[:div.center.menu
(let [menu (concat primary-menu secondary-menu)]
(for [i menu]
[:div.item {:key (:key i)}
[:a.item {:id (:key i)
:href (:target i)} (:name i)]]))
[:div.item
[:a
{:style {:cursor :pointer}
:id :logout
:on-click #(re-frame/dispatch [:logout])} (str "Logout")]]]]]))])]])
菜单按钮单击事件调度
:toggle-mobile-menu
re-frame/reg-event-db 切换菜单的可见性show_menu
绑定订阅re-frame/reg-sub:show-menu-mobilewhich gets the
:mobile-menu -来自 db 的可见值
数据库中的
@show-menu
值被解构为一个真值,指示菜单应该隐藏还是显示。