简体   繁体   English

如何使用Backbone.js停止事件传播?

[英]How can I stop event propagation with Backbone.js?

Using a Backbone.js View, say I want to include the following events: 使用Backbone.js视图,说我想要包含以下事件:

    events: {
        'click a': 'link', 
        'click': 'openPanel' 
    }

How can I avoid openPanel to be fired when I click on a link. 单击链接时,如何避免触发openPanel。 What I want is to have a clickable box which will trigger an action, but this box can have elements which should trigger other actions, and not the parent action. 我想要的是有一个可以触发动作的可点击框,但是这个框可以包含应该触发其他动作的元素,而不是父动作。 Think for example Twitter.com, and links in Tweets/right hand panel. 想想例如Twitter.com,以及推文/右侧面板中的链接。

I've been using e.stopImmediatePropagation(); 我一直在使用e.stopImmediatePropagation(); in order to keep the event from propagating. 为了防止事件传播。 I wish there was a shorter way to do this. 我希望有一个更短的方法来做到这一点。 I would like return false; 我想回归虚假; but that is due to my familiarity with jQuery 但那是因为我对jQuery很熟悉

The JQuery preventDefault method would also be a good option. JQuery preventDefault方法也是一个不错的选择。

    window.LocationViewLI = Backbone.View.extend({
        tagName: "li",
        template: _.template('<a href="/locations/<%= id %>"><%= name %></a>'),

        events: {
            "click a": "handleClick"
        },      
        handleClick: function(event) {
            event.preventDefault();
            console.log("LocationViewLI handleClick", this.model.escape("name") );
            // debugger;
        },
        ...

Each of your event handlers will be passed an event object when it's triggered. 每个事件处理程序在触发时都会传递一个事件对象。 Inside your handler, you need to leverage jQuery's event.stopPropagation() method. 在处理程序中,您需要利用jQuery的event.stopPropagation()方法。 For example: 例如:

link: function(event) {  
  //do some stuff here
  event.stopPropagation();
}

Two other methods that might work for you: 另外两种可能对您有用的方法:

1 1

events: {
    'click a': 'link', 
    'click *:not(a, a *)': 'openPanel' 
}

Then openPanel will not capture click events on any <a> or child of an <a> (in case you have an icon in your <a> tag). 然后openPanel不会捕捉click任何事件<a>一个或孩子<a> (如果你在你的图标<a>标签)。

2 2

At the top of the openPanel method, make sure the event target wasn't an <a> : openPanel方法的顶部,确保事件目标不是<a>

openPanel: function(event) {
    // Don't open the panel if the event target (the element that was
    // clicked) is an <a> or any element within an <a>
    if (event && event.target && $(event.target).is('a, a *')) return;

    // otherwise it's safe to open the panel as usual
}

Note that both of these methods still allow the openPanel function to be called from elsewhere (from a parent view or another function on this view, for example). 请注意,这两种方法仍然允许从其他地方调用openPanel函数(例如,从父视图或此视图上的其他函数)。 Just don't pass an event argument and it'll be fine. 只是不要传递一个event参数,它会没事的。 You also don't have to do anything special in your link function -- just handle the click event and move on. 您也无需在link功能中执行任何特殊操作 - 只需处理单击事件并继续操作即可。 Although you'll probably still want to call event.preventDefault() . 虽然您可能仍想调用event.preventDefault()

在“链接”功能中返回“false”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Backbone.js中,如何阻止事件传递到父视图? - In Backbone.js how do I stop an event from being passed to parent views? 如何使用jquery停止事件传播 - How can I stop event propagation with jquery 在backbone.js中收听集合的change事件时,如何判断哪个模型发生了变化 - How can I tell which model changed when listening to the change event of a collection in backbone.js 我如何在ID的click事件上在视图上调用函数,函数是写在骨架中的controller.js中的 - How can i call function on view on the click event of Id, function is written on controller in backbone.js 当其中一个模型被更改时,如何为Backbone.js中的集合视图附加事件处理程序? - How can I attach event handler for a collection view in Backbone.js when one of the models is changed? 在ribs.js中,如何捕获除纯“更改”事件以外的所有事件? - In backbone.js how can I catch all events except the plain “change” event? 如何使用骨干.js删除该项目中的元素? - How can I remove elements in this project with backbone.js? 如何使用Backbone.js获取Tweet时间轴? - How can I get Tweet Timeline with using Backbone.js? 如何使用Backbone.js实现“突出显示模式”? - How can I implement a “highlight mode” using Backbone.js? 如何更新集合中的所有模型-Backbone.js - How can I update all models in a collection - Backbone.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM