简体   繁体   中英

backbone.js unbind function from model when destroy view

I want to destroy view and make other view for a model. but I can not unbind functions binded to model from old view.

my test code

View = Backbone.View.extend({
    initialize: function(){
       this.model.bind('change',this.render);
    },

    destroy_view: function() {

       //COMPLETELY UNBIND THE VIEW
       this.undelegateEvents();

       this.$el.removeData().unbind();

       //Remove view from DOM
       this.remove();
       Backbone.View.prototype.remove.call(this);
   },
});

and I made new model and view, and I destroy view like this

view.destroy_veiw();
delete view;

but stil render is called when model changed

model.triger('change');

I know model.unbind(); can solve this problem , but it will unbind other functions , I want use this model for other view. How can I solve this ?

var View = Backbone.View.extend({

    initialize: function(){
        this.listenTo(this.model, 'change', this.render);            
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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