Backbone.js View can't unbind events properly

前端 未结 5 1941
执笔经年
执笔经年 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:06

    You could solve this another way

    var AppView = Backbone.View.extend({
        el:$("#app-view"),
        initialize:function(){
            _.bindAll(this,"cancel");
        },
    
        events:{
            "click .button":"do"
        },
    
        do:_.once(function(){
            console.log("do something...");
        })
    });
    var view = new AppView();
    

    underscore.js once function ensures that the wrapped function can only be called once.

提交回复
热议问题