0

我在使用 Backbone 为我的网站呈现购物袋视图时遇到问题。

我为所有包视图使用 1 个集合(项目列表的“Quick_View”和项目列表的“Normal_View”)。我还创建了“Item_View”,用于渲染两个视图中的每个项目。

它是一个 SPA(单页应用程序),“Quick_View”为所有主干路由启动和渲染,默认情况下是隐藏的。每当用户从它显示的任何页面单击“Quick_View”链接时。没有为它定义路由。

“Normal_View”,可以使用“Quick_View”中给出的 Checkout 按钮访问。它与“域/结帐”路由绑定。

当我从“Quick_View”检查按钮访问“Normal_View”时;它工作正常,两个(快速和正常)视图都处于同步状态。意味着,当我们添加、删除、更新任何视图中的任何项目时,两个视图都会相应地更新。

但是当我直接在新浏览器中访问“域/结帐”路由时,两个视图都可以正常渲染,但它们不同步。意味着,在 1 个视图中的更改不会更新另一个视图。

我跟踪的原因是,当我通过“Quick_View”访问“Normal_View”时,两个视图中的每个项目的模型都具有相同的 CID,因此两个视图是同步的,如果模型中的任何一个有任何变化看法。

而且,当我直接访问“Normal_View”时,两个视图中每个项目的模型都没有相同的 CID,因此它们无法按预期工作。

还有几点需要考虑:

  • 集合为“Quick_View”触发了两次重置事件,并且“Quick_View”中的每个项目都渲染了两次。
  • 当我访问“Normal_View”(以任何一种方式)时,“Quick_View”再次被渲染,但一旦“Normal_View”渲染结束。

// 主视图

var mainView = Backbone.View.extend({
    el: 'body',
    template: {
        header: Handlebars.compile(headerTemplate),
        mainNav: Handlebars.compile(mainNavtemplate),
        footer: Handlebars.compile(footerTemplate)
    },
    initialize: function() {
        _.bindAll();
        AW.collection.bag = new bagCollection();
        //AW.collection.bag.fetch({reset:true});
    },
    render: function() {
        this.$el.html(this.template());
        this.loadSubView('bagQV');
    },
    loadSubView: function(subView) {
        switch(subView) {
            case 'home' :
                if(!AW.view.home) AW.view.home = new homepageView();
                AW.view.home.render();
                break;
            case 'bagQV'    :
                if(!AW.view.bagQV) AW.view.bagQV = new bagQuickView({collection: AW.collection.bag});
                //AW.view.bagQV.render();
                break;
            case 'checkout' :
                if(!AW.view.checkout) AW.view.checkout = new checkoutView({collection: AW.collection.bag});
                AW.view.checkout.render();
                break;
        }
    }
});

// 单项视图

var bagItemView = Backbone.View.extend({
    tagName: 'tr',
    template:   Handlebars.compile(bagItemTemplate),
    initialize: function() {
        _.bindAll(this);
        this.listenTo(this.model, 'change', this.render);
        this.listenTo(this.model, 'remove', this.removeItem);
        $(document).on('keyup', this.listenKeyboard);
    },
    events: {
        'click .qtyInput .button'           : 'updateItem',
        'click .controls a.remove'          : 'removeModel'
    },
    render: function() {
        this.$el.html(this.template(this.model.toJSON()));
        this.$el.attr('data-id',this.model.cid);            
        return this;
    },
    updateItem: function(e) {
        e.preventDefault();
        e.stopPropagation();
        var newQty = this.$el.find('.qtyInput input').val();
        var newAmt = newQty * parseFloat(this.model.get('prodRate').replace('$',''));
        this.model.set({prodQty: newQty, amount: '$' + newAmt});
        this.cancelEdit(e);
    },
    removeModel: function(e) {
        e.preventDefault();
        e.stopPropagation();
        if(AW.collection.bag) AW.collection.bag.remove(this.model);

    },
    removeItem: function() {
        this.$el.remove();
    }
});

// 包快速查看

var bagQuickView = Backbone.View.extend({
    tagName:    'div',
    id:         'myBagQV',
    template:   Handlebars.compile(bagQuickViewTemplate),
    initialize: function() {
        _.bindAll(this);
        this.collection.fetch({reset:true});
        //this.collection.bind("reset", _.bind(this.render, this));
        this.listenTo(this.collection, 'add', this.addItem);
        this.listenTo(this.collection, 'reset', this.render);
    },
    render: function() {
        if($('#myBagQV').length == 0) {
            this.$el.html(this.template());
            $('body').append(this.el);
        }
        this.addAllItems();
        return this;
    },
    addItem: function(item) {
        var parent = this;
        var itemView = new bagItemView({model: item});
        $('#itemsInBag table tbody').append(itemView.render().el);
    },
    addAllItems: function() {
        if(this.collection.length > 0) {
            $('#itemsInBag table tbody').html('');
            this.collection.each(this.addItem, this);
        }
    },
});

// 普通包视图

var bagView = Backbone.View.extend({
    tagName:    'div',
    id:         'myBag',
    template:   Handlebars.compile(checkoutBagTemplate),
    initialize: function() {
        _.bindAll(this);
        this.collection.fetch({reset:true});
        //this.collection.bind("reset", _.bind(this.render, this));
        this.listenTo(this.collection, 'add', this.addItem);
        this.listenTo(this.collection, 'reset', this.render);
    },
    render: function() {
        this.$el.html(this.template());
        $('#checkoutContainer #details').append(this.el);
        this.addAllItems();
        return this;
    },
    addItem: function(item) {
        var parent = this;
        var itemView = new bagItemView({model: item});
        this.$el.find('table tbody').append(itemView.render().el);
    },
    addAllItems: function() {
        if(this.collection.length > 0) {
            this.$el.find('table tbody').html('');
            this.collection.each(this.addItem, this);
        }
    }
});

找你帮忙。先感谢您

干杯,维克拉姆

4

0 回答 0