我有簡單的視圖,顯示對話框。爲什麼Backbone.View對象仍然保留在內存中?
Backbone.View.prototype.completeRemove = function(){
this.undelegateEvents();
this.remove();
delete this.$el;
delete this.el;
console.log('completely removed')
}
MdApp.dialogBox = Backbone.View.extend({
defaults: {
text: __('No text provided'),
buttonText: __('Ok'),
callback: function(){
return null;
},
el: $('#app-panel'),
type: 'error',
cancellable: false,
cancelText: __('No'),
picture: pic('default')
},
el: '<div class="dialog-box">',
template: _.template($('#dialog-box-template').html()),
events: {
'click .confirm' : 'confirm',
'click .cancel' : 'cancel'
},
initialize: function(){
this.model = _.extend(this.defaults, this.model);
this.render();
},
render: function(){
var model = this.model;
this.$el.html(this.template(model));
model.el.append(this.el);
},
confirm: function(){
var model = this.model;
var view = this;
this.completeRemove();
model.callback();
},
cancel: function(){
this.completeRemove();
}
});
它有它自己的默認值。每次我初始化新對話框時,它的值在每次對話框調用之間都會持續存在。例如,當我調用對話首次:
new MdApp.dialogBox({model:{
text: __('Do you really wanna delete this?'),
buttonText: __('Unfortunately yes'),
callback: function(){
//some callback
},
cancellable: true,
cancelText: __('No'),
picture: pic('confirm delete')
}});
在那之後,我調用另一個對話框,不cancellable
屬性,因此它應該使用默認的(這是false
),但它保持真實。這適用於所有其他財產。爲什麼會發生?