简体   繁体   English

backbone.js在销毁视图时解除模型的绑定功能

[英]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 但是当模型改变时调用stil渲染

model.triger('change');

I know model.unbind(); 我知道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);            
    }
});

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

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