Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ClojureScript项目中使用clojure.browser.event.unlisten_Clojure_Clojurescript - Fatal编程技术网

在ClojureScript项目中使用clojure.browser.event.unlisten

在ClojureScript项目中使用clojure.browser.event.unlisten,clojure,clojurescript,Clojure,Clojurescript,我正在尝试编写clojureScript One项目,但在删除事件侦听器unlisten时遇到问题-侦听器已使用以下代码注册: (defn- add-expand_fold-listener "Accepts a ele-id and creates listeners for click events on div which will then fire rendering changes" [ele-id] (log/log "adding opening listeners") (eve

我正在尝试编写clojureScript One项目,但在删除事件侦听器unlisten时遇到问题-侦听器已使用以下代码注册:

(defn- add-expand_fold-listener
"Accepts a ele-id and creates listeners for click events on div
which will then fire rendering changes"
[ele-id]
(log/log "adding opening listeners")
(event/listen (single-node (by-id ele-id))
    "click"
#(dispatch/fire (re-class ele-id "foldup" "expand"))))
(defn- remove-expand_fold-listener
 "Accepts a ele-id and removes listener for click events on div"
 [ele-id]
 (log/log "removing opening listener")
(event/unlisten (by-id ele-id) 
      "click" 
      #(dispatch/fire (re-class ele-id "foldup" "expand")) 
      false)
(log/log "done removing listener"))
但当我试图用这段代码来解压时:

(defn- add-expand_fold-listener
"Accepts a ele-id and creates listeners for click events on div
which will then fire rendering changes"
[ele-id]
(log/log "adding opening listeners")
(event/listen (single-node (by-id ele-id))
    "click"
#(dispatch/fire (re-class ele-id "foldup" "expand"))))
(defn- remove-expand_fold-listener
 "Accepts a ele-id and removes listener for click events on div"
 [ele-id]
 (log/log "removing opening listener")
(event/unlisten (by-id ele-id) 
      "click" 
      #(dispatch/fire (re-class ele-id "foldup" "expand")) 
      false)
(log/log "done removing listener"))
代码运行时没有错误,但侦听器没有被删除,我想我有语法错误,但我不确定在哪里

任何帮助都将不胜感激。
在DOM中,您可以将多个事件处理程序附加到单个元素,因此当您要删除事件处理程序时,DOM需要知道您要删除哪个特定的事件处理程序。在您的代码中,附加事件处理程序会创建一个新的处理程序函数并附加它,而在另一个代码中,它会创建另一个新函数,尽管代码是相同的并尝试取消对它的侦听,因此基本上它不起作用,因为您需要将绑定事件时使用的相同函数对象传递给unlisten

要解决这个问题,您需要将处理程序定义为一个名称不是匿名的函数,并在绑定和取消绑定事件中使用该函数

(defn myHandler [& args] (dispatch/fire (re-class ele-id "foldup" "expand")))

在DOM中,可以将多个事件处理程序附加到单个元素,因此当您要删除事件处理程序时,DOM需要知道要删除哪个特定的事件处理程序。在您的代码中,附加事件处理程序会创建一个新的处理程序函数并附加它,而在另一个代码中,它会创建另一个新函数,尽管代码是相同的并尝试取消对它的侦听,因此基本上它不起作用,因为您需要将绑定事件时使用的相同函数对象传递给unlisten

要解决这个问题,您需要将处理程序定义为一个名称不是匿名的函数,并在绑定和取消绑定事件中使用该函数

(defn myHandler [& args] (dispatch/fire (re-class ele-id "foldup" "expand")))

解决方案:与其尝试使用domina,这是一个好东西,但并非所有的函数都在lein deps中,还不如尝试使用clojureScript一个事件模型,最终直接找到了源代码。。。添加到命名空间的require部分:

[goog.events :as g-events]
然后在使用的删除功能中:

(g-events/removeAll (by-id ele-id) "click")
它就像一个冠军。
希望这对将来的人有所帮助。

解决了:与其尝试使用domina,这是一个好东西,但并非所有函数都在lein deps中,还不如尝试使用clojureScript One事件模型,最终找到了源代码。。。添加到命名空间的require部分:

[goog.events :as g-events]
然后在使用的删除功能中:

(g-events/removeAll (by-id ele-id) "click")
它就像一个冠军。
希望这对将来的人有所帮助…

很遗憾,这不起作用。似乎我无法使用这样的函数绑定到事件,我尝试了很多方法,但它只在匿名使用时添加了侦听器,因此从未使用过unlisten部分。谢谢AnkurSadly,不起作用。似乎我无法使用这样的函数绑定到事件,我尝试了很多方法,但它只在匿名使用时添加了侦听器,因此从未使用过unlisten部分。谢谢Ankur