多个视图绑定到backbone.js中的单个元素

发布于 2024-12-20 07:29:55 字数 198 浏览 0 评论 0 原文

我试图将两个单击事件绑定到两个不同视图中的单个 HTML 元素。其中一个视图触发事件,另一个则不会。

其中一个视图将 body 作为其 el 属性。如果我将此视图的 el 更改为与其他视图相同的元素,则两个事件都会被触发。

这是预期的吗?如何在两个不同视图中绑定同一元素的点击事件?

I am trying to bind two click events to a single HTML element in two different views. One of the views triggers the event, the other does not.

One of the view has body as its el attribute. If I change this view's el to the same element as the other view's, then both events get triggered.

Is this expected? How can I bind click events for the same element in two different views?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2024-12-27 07:29:55

是的,这是预期的。 Backbone 使用 jQuery 委托 进行事件绑定。这意味着,事件实际上绑定到视图的 EL,而不是直接绑定到子节点。

当您说“相同的元素”时,您的意思实际上是 DOM 中完全相同的节点吗?或者,您是指具有相同选择器的节点吗?我想我还不太清楚。

Yes, this is expected. Backbone uses jQuery delegates for the event binding. Which means, the event is actually bound to the view's EL, not directly to the child node.

When you say, "the same element", do you mean literally the exact same node in the DOM? Or, do you mean a node with the same selector? I guess I'm not entirely clear.

辞别 2024-12-27 07:29:55

我可以问为什么你想要将 2 个视图绑定到同一个元素吗?

从我的角度来看,你应该只有 1 个代表元素本身的视图
并且绑定到元素的事件应该仅在该视图中定义。

当您将单击事件绑定到不属于视图的元素时,您会遇到麻烦

如果您通过 delegateEvents 哈希,这些事件包含在视图的 el 中。
然而,如果您自己定义点击,您的代码将变得更难以管理。

因此,关于您可以做什么:

事件!

您可以定义 1 个视图,按住按钮并在单击按钮时触发事件,而按下该按钮时需要处理某些代码的其他视图不会直接绑定到按钮单击本身,它们可以侦听该引发的事件。

jsfiddle 上的示例:
http://jsfiddle.net/saelfaer/Qck5w/2/

代码中的要点这里:

// an event aggregator object to trigger and bind to
var events = _.extend({}, Backbone.Events),

// two views that talk to each other trough the event aggregator
var myButtonView = Backbone.View.extend({
    // first view binds a click event to the button
    events: {
        "click a" : "myClickEvent"
    },
    initialize: function(){
        _.bindAll(this, "render");
        this.render();
    },
    render: function(){
        return this;
    },
    // click event executes this function, which triggers a custom event on the events object.
    myClickEvent: function(e){
        $(e.target).blur();
        events.trigger("sidebar:myCustomClickEvent");
        return false;
    }
});

var myPanelView = Backbone.View.extend({
    // second view binds to that event, and executes the custom click handler
    initialize: function(){
        _.bindAll(this, "render", "myClickEventHandler");
        events.bind("sidebar:myCustomClickEvent", this.myClickEventHandler);
        this.render();
    },
    render: function(){
        return this;
    },
    // the click handler does some logic (appends div) when the event is raised.
    myClickEventHandler: function(){
        var txt = $('<div/>').text("you just clicked the button. (bound in other view)");
        $(this.el).append(txt);
    }
});

can i ask why you want to have 2 views binding to the same element?

from my point of view, you should only have 1 view that represents the element itself
and event's bound to an element should be defined in that view only.

you will run into trouble when you are binding click events to elements that don't belong to the view

if you bind trough the delegateEvents hash, these events are contained within the el of the view.
if you are however defining the click yourself, your code becomes less managable.

so, on to what you can do:

events!

you can define 1 view, holding your button and trigger an event when the button is clicked, while other views that need to handle some code when that button is pressed don't bind directly to the button click itself, they can listen to that raised event.

example on jsfiddle:
http://jsfiddle.net/saelfaer/Qck5w/2/

the gist of it in code here:

// an event aggregator object to trigger and bind to
var events = _.extend({}, Backbone.Events),

// two views that talk to each other trough the event aggregator
var myButtonView = Backbone.View.extend({
    // first view binds a click event to the button
    events: {
        "click a" : "myClickEvent"
    },
    initialize: function(){
        _.bindAll(this, "render");
        this.render();
    },
    render: function(){
        return this;
    },
    // click event executes this function, which triggers a custom event on the events object.
    myClickEvent: function(e){
        $(e.target).blur();
        events.trigger("sidebar:myCustomClickEvent");
        return false;
    }
});

var myPanelView = Backbone.View.extend({
    // second view binds to that event, and executes the custom click handler
    initialize: function(){
        _.bindAll(this, "render", "myClickEventHandler");
        events.bind("sidebar:myCustomClickEvent", this.myClickEventHandler);
        this.render();
    },
    render: function(){
        return this;
    },
    // the click handler does some logic (appends div) when the event is raised.
    myClickEventHandler: function(){
        var txt = $('<div/>').text("you just clicked the button. (bound in other view)");
        $(this.el).append(txt);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文