简体   繁体   English

初始化后添加backbone.js事件

[英]Adding backbone.js events after initialization

This code does not have much context, but will illustrate my question. 这段代码没有太多的上下文,但会说明我的问题。 It's a Backbone view defined in an AMD module which I use to load via require.js 这是我在AMD模块中定义的Backbone视图,我用它来通过require.js加载

Essentially I want to be able to add events to a Backbone view after initialization has been run. 基本上我希望能够在运行初始化后将事件添加到Backbone视图。 At the moment I am making an empty events object, and then in a method called addView, I add an event each time the function is called. 目前我正在创建一个空事件对象,然后在一个名为addView的方法中,每次调用该函数时都会添加一个事件。 This is not currently working. 这目前无效。

I'm not sure at what point the events are registered, but I'm suspecting that I somehow have to get the view to update it's listeners when the events object changes? 我不确定事件在什么时候被注册,但是我怀疑在events对象发生变化时我不得不让视图更新它的监听器?

Does anybody know how I can achieve adding events to a Backbone.js after initialization? 有没有人知道如何在初始化后向Backbone.js添加事件?

   define([
    'jQuery',
    'Backbone'],

    function ($, Backbone) {

        var contentViews = new Array();

        var SlidePanelView = Backbone.View.extend({

            events: {},

            /// <param name="targetDataAtt">The unique value inside the target's Data-Slide-Panel-Id attribute</param>  
            /// <param name="event">Backbone event to trigger view</param>
            /// <param name="destroyOnRemove">True to destroy view when removed, False otherwise (Default true)</param>  
            addView: function (targetDataAtt, event, view, destroyOnRemove) {
                destroyOnRemove = typeof destroyOnRemove !== 'undefined' ? destroyOnRemove : true;

                this.events[event] = "viewEventFired";
                contentViews[targetDataAtt] = { View: view, DestroyOnRemove: destroyOnRemove };
            },

            viewEventFired: function (e) {

                var target = $(e.target);
                var id = target.attr('data-slide-panel-id');
                console.log(id);
            }

        });

        // Return a singleton??
        return new SlidePanelView;


    });

If I'm understanding the question correctly, after you add additional events to the events hash, call delegateEvents() on the view to re-bind events. 如果我正确理解了这个问题,在向事件哈希中添加其他事件后,请在视图上调用delegateEvents()以重新绑定事件。

    addView: function (targetDataAtt, event, view, destroyOnRemove) {
       destroyOnRemove = typeof destroyOnRemove !== 'undefined' ? destroyOnRemove : true;
       this.events[event] = "viewEventFired";
       this.delegateEvents();
       contentViews[targetDataAtt] = { View: view, DestroyOnRemove: destroyOnRemove };
    }

It does unbind all the existing event bindings and rebinds all the events in the events hash again, and I'm not sure of the underlying performance issues of doing that, but its something to be aware of. 它解除了所有现有事件绑定的绑定,并再次重新绑定事件哈希中的所有事件,我不确定这样做的潜在性能问题,但需要注意的事项。

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

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