【问题标题】:How to bind and unbind mouse events on Marionette如何在 Marionette 上绑定和取消绑定鼠标事件
【发布时间】:2013-12-29 20:24:25
【问题描述】:

我认为有这个事件:

    events:
        "click" : "clickContainer"

如何在同一个视图中临时解除/绑定(能够和禁用)点击事件?

【问题讨论】:

    标签: events backbone.js bind unbind marionette


    【解决方案1】:

    我会在视图上有一个属性。像这样的:

    var View = Marionette.ItemView.extend({
        initialize: function() {
            this.clickEnabled = true;
        },
        events: {
            'click': 'clickContainer'
        },
        clickContainer: function() {
            if ( this.clickEnabled ) {
               // do stuff
            }
        }
    });
    

    那么当你想改变状态时,你只需改变那个属性。

    【讨论】:

      【解决方案2】:

      另一种选择是删除事件映射并使用事件映射设置的手动版本。

      onShow: function() {
        this.enableClick();
      },
      
      enableClick: function() {
        $(".clickContainer").on("click", this.onClickContainer);
      },
      
      disableClick: function() {
        $(".clickContainer").off("click", this.onClickContainer);
      },
      
      onClickContainer: function() {
        // do stuff
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多