取消绑定骨干查看事件 [英] Unbind Backbone View Events

查看:100
本文介绍了取消绑定骨干查看事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下降的事件哈希 -

I have the falling events hash -

events:
  'click #someButton : 'someFunction'

要关闭我尝试过的视图

close:
  $("#someButton").unbind("click")

   `close:

       $("#someButton").remove()`

但someFunction仍然被触发了不止一次。如何从按钮中取消绑定此事件?

But someFunction is still being fired more than once. How do I unbind this event from the button?

我也尝试过

$(@el).find("#someButton").unbind("click") as well 


推荐答案

Backbone.js视图事件被委派给视图的 el (所以没有事件绑定到你的 #someButton 元素,而当点击事件冒泡到 el 时,它会检查事件是否来自与该选择符匹配的元素),删除您需要将其从 el 中删除​​的事件,例如

Backbone.js view events are delegated to the view's el (so there is no event bound to your #someButton element but rather when a click event bubbles up to the el it checks to see if the event came from an element matching that selector), that being the case to remove the event you need to remove it from the el, for example

  $(this.el).off('click', '#someButton');

如果要删除所有委派的事件,您只需使用视图的 undelegate 方法

If you want to remove all delegated events you can just use the view's undelegate method

这篇关于取消绑定骨干查看事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆