简体   繁体   English

Backbone.js视图 - 将事件绑定到“el”之外的元素

[英]Backbone.js views - binding event to element outside of “el”

The 2nd answer to this question nicely explains how event declarations in Backbone.js views are scoped to the view's el element. 这个问题的第二个答案很好地解释了Backbone.js视图中的事件声明如何作用于视图的el元素。

It seems like a reasonable use case to want to bind an event to an element outside the scope of el , eg a button on a different part of the page. 想要将事件绑定到el范围之外的元素(例如页面的不同部分上的按钮)似乎是合理的用例。

What is the best way of achieving this? 实现这一目标的最佳方法是什么?

there is not really a reason you would want to bind to an element outside the view, there are other methods for that. 你想要绑定到视图外的元素并不是真正的理由,还有其他方法可以解决这个问题。

that element is most likely in it's own view, (if not, think about giving it a view!) since it is in it's own view, why don't you just do the binding there, and in the callback Function, use .trigger(); 该元素最有可能出现在它自己的视图中(如果没有,请考虑给它一个视图!)因为它在它自己的视图中,为什么不在那里进行绑定,在回调函数中,使用.trigger(); to trigger an event. 触发事件。

subscribe to that event in your current view, and fire the right code when the event is triggered. 在当前视图中订阅该事件 ,并在触发事件时触发正确的代码。

take a look at this example in JSFiddle, http://jsfiddle.net/xsvUJ/2/ 看一下JSFiddle中的这个例子, http://jsfiddle.net/xsvUJ/2/

this is the code used: 这是使用的代码:

var app  = {views: {}};

app.user = Backbone.Model.extend({
    defaults: { name: 'Sander' },
    promptName: function(){
        var newname = prompt("Please may i have your name?:");
        this.set({name: newname});
    }
});

app.views.user = Backbone.View.extend({
    el: '#user',
    initialize: function(){
        _.bindAll(this, "render", "myEventCatcher", "updateName");
        this.model.bind("myEvent", this.myEventCatcher);
        this.model.bind("change:name", this.updateName);
        this.el = $(this.el);
    },
    render: function () {
        $('h1',this.el).html('Welcome,<span class="name">&nbsp;</span>');
        return this;
    },
    updateName: function() {
        var newname = this.model.get('name');
        console.log(this.el, newname);
        $('span.name', this.el).text(newname);
    },
    myEventCatcher: function(e) {
        // event is caught, now do something... lets ask the user for it's name and add it in the view...
        var color = this.el.hasClass('eventHappened') ? 'black' : 'red';
        alert('directly subscribed to a custom event ... changing background color to ' + color);
        this.el.toggleClass('eventHappened');

    }
});

app.views.sidebar = Backbone.View.extend({
    el: '#sidebar',
    events: {
        "click #fireEvent" : "myClickHandler"
    },
    initialize: function(){
        _.bindAll(this, "myClickHandler");
    },
    myClickHandler: function(e) {
        window.user.trigger("myEvent");
        window.user.promptName();
    }
});


$(function(){
    window.user = new app.user({name: "sander houttekier"});
    var userView = new app.views.user({model: window.user}).render();
    var sidebarView = new app.views.sidebar({});
});

Update: This answer is no longer valid/right. 更新:此答案不再有效/正确。 Please see other answers below! 请看下面的其他答案!

Why do you want to do this? 你为什么要这样做?

Apart from that, you could always just bind it using regular jQuery handlers. 除此之外,你总是可以使用常规的jQuery处理程序绑定它。 Eg 例如

$("#outside-element").click(this.myViewFunction);

IIRC, Backbone.js just uses the regular jQuery handlers, so you're essentially doing the same thing, but breaking the scope :) IIRC,Backbone.js只使用常规的jQuery处理程序,所以你基本上做同样的事情,但打破范围:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM