Javascript 页面在清除主干视图数组时被挂起
我正在通过以下代码清除主干阵列,但我看到在执行this.views=[]时浏览器被挂起Javascript 页面在清除主干视图数组时被挂起,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我正在通过以下代码清除主干阵列,但我看到在执行this.views=[]时浏览器被挂起 resetView : function(){ this.views = []; }, 有没有人遇到过类似的问题。在某些情况下,浏览器会在几分钟后继续运行,但在大多数情况下,我会收到一个无响应的脚本警报框 这是我的全部代码 initbrands: function(){ if(!app.views.brands) app.views.brands = new app.View
resetView : function(){
this.views = [];
},
有没有人遇到过类似的问题。在某些情况下,浏览器会在几分钟后继续运行,但在大多数情况下,我会收到一个无响应的脚本警报框
这是我的全部代码
initbrands: function(){
if(!app.views.brands)
app.views.brands = new app.View.TabContainer({
collection: new app.Collection.TabBrand(),
ViewModel: app.View.Brand,
MenuTemplate: _.template($('#brands-menu').html()),
TabHeaderTemplate : _.template($('#brands-header').html())
});
return app.views.brands;
},
initAddBrand: function(listView){
if(!app.views.addbrand){
app.views.addbrand = new app.View.AddBrand();
app.views.addbrand.bind('successBrands', listView.collection.fetch, listView.collection);
}
return app.views.addbrand;
},
brands: function(){
var view = this.initbrands();
this.selectMenu('brands');
view.collection.owned();
var addview = this.initAddBrand(view);
addview.collection = view.collection;
if(this.views.indexOf(view)===-1){
this.resetView();
this.addView(addview);
this.addView(view);
}
view.selectMenu('brands');
},
editBrand: function(id){
this.selectMenu('brands');
this.resetView();
var brand = new app.Model.Brand({
brandId: id,
});
brand.url = '/rest/brands/'+id,
brand.fetch();
this.addView(new app.View.EditBrand({
model: brand
}));
},
app.Model.Brand = Backbone.Model.extend({
idAttribute: 'brandId',
defaults: {
name: '',
description: '',
brandImage:'',
user: '',
showPro: false,
proDescription: '',
proBrandImage1:'',
proBrandImage2:'',
proBrandImage3:''
}
});
app.View.AddBrand = Backbone.View.extend({
tagName: 'form',
id: "addBrandToUser",
initialize: function(){
this.$el.addClass('form-horizontal row-fluid');
this.model = new app.Model.Brand();
},
template:_.template($('#brands-form').html()),
events:{
'click .show': 'toggle',
'submit': 'submit',
'reset': 'toggle'
},
toggle: function(){
this.$el.find('fieldset').toggle();
},
render: function(){
this.delegateEvents();
this.$el.html(this.template(this.model.toJSON()));
return this.$el;
},
submit: function(e){
e.preventDefault();
var form = $(e.target);
this.model.set('name', form.find('[name="name"]').val());
this.model.set('description', form.find('[name="description"]').val());
this.model.set('showPro', form.find('[name="showPro"]:checked').val() === 'true');
this.model.set('proDescription', form.find('[name="proDescription"]').val());
this.model.url="/rest/brand/create";
this.model.save(null, {
success: function(){
$(e.target)[0].reset();
self.trigger('successBrands');
},
error: function(){
}
});
}
});
app.Collection.TabBrand = Backbone.Collection.extend({
model : app.Model.Brand,
initialize: function(){
this.options= {};
this.options.url = {
owned: '/rest/brands',
};
},
owned: function(){
this.url = this.options.url.owned;
this.parse = function(data, options){
return data;
};
this.fetch();
}
});
app.View.Brand = Backbone.View.extend({
initialize: function(){
},
template:_.template($('#brands-item').html()),
tagName: 'tr',
events:{
'click .removeBrand': 'remove'
},
remove: function(){
var sd = new app.Model.BrandDelete(this.model);
sd.destroy(null, {
success: function(){
}
});
},
render: function(){
var self = this;
var data = this.model.toJSON();
this.$el.html(this.template(data));
this.delegateEvents();
return this.$el;
}
});
app.View.EditBrand = Backbone.View.extend({
tagName: 'form',
attributes : {
'class' : 'form-horizontal row-fluid'
},
initialize: function(){
this.model.bind('change', this.render, this);
},
template:_.template($('#brands-form').html()),
events:{
'submit': 'submit'
},
render: function(){
this.delegateEvents();
this.$el.html(this.template(this.model.toJSON()));
},
submit: function(e){
e.preventDefault();
var form = $(e.target);
this.model.set('name', form.find('[name="name"]').val());
this.model.set('description', form.find('[name="description"]').val());
this.model.set('proDescription', form.find('[name="proDescription"]').val());
this.model.set('showPro', form.find('[name="showPro"]:checked').val() === 'true');
this.model.url="/rest/brand/update";
var self = this;
self.model.save(null, {
success: function(){
},
error: function(){
}
});
}
});
你确定
这个就是你所想的吗?为什么你认为这一行是问题所在?是的,因为我使用firebug进行了调试,当涉及到这一行时,它会挂起。我在下一行中设置了一个断点,其余的执行工作正常,这就是您所期望的?resetView
是如何调用的?是的,我确定。。我还交叉检查了它并看到了这个。视图在数组中包含两个元素。您检查过内存泄漏或类似的情况吗?重置该阵列可能会触发其他问题,而不是真正的问题。