如何使用试剂打ic添加点击事件以触发ClojureScript中的JavaScript事件

时间:2018-07-17 04:55:07

标签: javascript clojure clojurescript 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!"]])

1 个答案:

答案 0 :(得分:1)

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

(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 重新构架/ reg-event-db ,用于切换菜单的可见性

  • show_menu绑定订阅 re-frame / reg-sub :show-menu-mobile which gets the:mobile-menu-visible`值db

  • 数据库中的@show-menu值被分解为真实值,指示该菜单是应该隐藏还是显示。

相关问题