Backbone.js View can't unbind events properly

前端 未结 5 1919
执笔经年
执笔经年 2020-12-25 08:11

I have some Backbone.js code that bind a click event to a button, and I want to unbind it after clicked, the code sample as below:

var AppView = Backbone.Vie         


        
5条回答
  •  醉梦人生
    2020-12-25 09:10

    you can simply use object.off, the code below is work for me

    initialize:function () {
    
        _.bindAll(this, 'render', 'mouseover', 'mouseout', 'delete', 'dropout' , 'unbind_mouseover', 'bind_mouseover');
        .......
    
    },
    
    events: {
        'mouseover': 'mouseover',
        'unbind_mouseover': 'unbind_mouseover',
        'bind_mouseover': 'bind_mouseover',
        .....
    },
    
    mouseover: function(){
        $(this.el).addClass('hover');
        this.$('.popout').show();
    },
    
    unbind_mouseover: function(){
        console.log('unbind_mouseover');
        $(this.el).off('mouseover');
    },
    bind_mouseover: function(){
        ........
    },
    

提交回复
热议问题