2

在此处输入图像描述我正在通过以下代码清除主干数组,但是当 this.views = [] 正在执行时,我看到浏览器被挂起。

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(){

        }
    });
}
});

在此处输入图像描述

4

0 回答 0