如何使用 Reagent Hiccup 在 ClojureScript 中添加点击事件以触发 JavaScript 事件

问题描述 投票:0回答:1

我刚开始使用 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!"]])
javascript clojure clojurescript reagent hiccup
1个回答
3
投票

在同事的帮助下,我们添加了以下内容。我不确定我是否已经正确解释了这一点。

(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
    绑定订阅了来自 dbre-frame/reg-sub
    :show-menu-mobile
    which gets the

    :mobile-menu-visible` 值
  • 来自数据库的

    @show-menu
    值被解构为一个真值,指示菜单是否应该隐藏或显示。

© www.soinside.com 2019 - 2024. All rights reserved.