拆散骨干观活动 [英] Unbind Backbone View Events

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

问题描述

我有坠落事件哈希 -

 事件:
  点击#someButton:someFunction

要关闭我曾尝试视图

 关闭:
  $(#someButton)。解除绑定(点击)

 `关闭:       $(#someButton)。remove()方法`

但是someFunction仍被烧制不止一次。怎样解除绑定按钮这一事件?

我也试过

  $(@ EL).find(#someButton)。解除绑定(点击),以及


解决方案

Backbone.js的观看赛事都被委托给视图的(所以没有必然的事件 #someButton 元素,而是当一个click事件冒泡到它检查事件是否从来到元素选择匹配),既然如此删除您需要从,例如:

删除事件

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

如果您要删除所有委托事件,你可以只使用视图的 undelegate 方法

I have the falling events hash -

events:
  'click #someButton : 'someFunction'

To close the view I have tried

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

and

   `close:

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

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

I've also tried

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

解决方案

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');

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

这篇关于拆散骨干观活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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