0

在我的主干功能中,如何在添加新元素之前删除元素?,我的功能一切正常。问题是当我单击“更新”时,它正在更新元素,并且仍然可以使用旧版本。如何删除旧元素,并从更新调用中追加新元素..?

清除现有视图元素并附加新集合的正确方法是什么?

我的功能:

$(document).ready(function(){

    var school = {};

    school.model = Backbone.Model.extend({
        defaults:{
            name:'no name',
            age:'no age'
        }
    });

    school.collect = Backbone.Collection.extend({
        model:school.model,
        url:'js/school.json'
    });

    school.view = Backbone.View.extend({
        tagName:'div',
        className:'member',
        template:$('#newTemp').html(),
        render:function(){
            var temp = _.template(this.template);
            this.$el.html(temp(this.model.toJSON()));
            return this;
        }
    });

    school.views = Backbone.View.extend({
        el:$('#content'),
        events:{
            'click #newData' : 'newArrival',    
        },
        initialize:function(){
            _.bindAll(this);
            this.collection = new school.collect;
            this.collection.bind('reset', this.render);
            this.collection.fetch();
        },
        newArrival:function(){
            school.views.remove(); // i tried this, throw the error 
            this.initialize();
        },
            render:function(){
                var that = this;
                _.each(this.collection.models, function(item){
                    that.renderItem(item);    
                })
            },
            renderItem:function(item){
                //how can i remove the older elements and append new alone?
                var newItem = new school.view({model:item});
                this.$el.append(newItem.render().el); // keep adding but previous element still not removed, 
            }
        });

        var newSchool = new school.views;

})
4

1 回答 1

1

您需要参考旧视图才能删除它们。然后,您将能够在子视图上调用 remove。我强烈建议您使用主干删除方法,而不是仅使用 jquery 从 DOM 中删除元素。使用 Backbones remove 也会解除对象与事件的绑定。要从服务器更新,只需调用 collection.fetch() 以获取新数据。这是我的解决方案(底部的 jsfiddle):

var school = {};

school.model = Backbone.Model.extend({
    defaults:{
        name:'no name',
        age:'no age'
    }
});

school.collect = Backbone.Collection.extend({
    model:school.model,
    url:'js/school.json'
});

school.view = Backbone.View.extend({
    tagName:'div',
    className:'member',
    template:$('#newTemp').html(),
    render:function(){
        var temp = _.template(this.template);
        this.$el.html(temp(this.model.toJSON()));
        return this;
    }
});

school.views = Backbone.View.extend({
    el:$('#content'),
    events:{
        'click #newData' : 'newArrival',    
    },
    initialize:function(){
        _.bindAll(this);
        this.collection = new school.collect;
        this.collection.bind('reset', this.render);
        this.collection.fetch();

        this.childViews = [];
    },
    newArrival:function(){
        this.collection.fetch();
    },
    render:function(){
        var that = this;

        // Remove old items
        _.each(this.childViews, function(old){
            old.remove();    
        });

        // Empty array of children
        this.childViews = [];

        var that = this;
        _.each(this.collection.models, function(item){
            that.renderItem(item);    
        });
    },
    renderItem:function(item){
        //how can i remove the older elements and append new alone?
        var newItem = new school.view({model:item});
        this.$el.append(newItem.render().el); // keep adding but previous element still not removed, 

        // Store a reference to your child item.
        this.childViews.push(newItem);
    }
});

var newSchool = new school.views;

我创建了一个工作:JSFiddle。注意:在 JSFiddle 集合中,我重写 fetch() 以提供假数据(您应该删除该部分)。

于 2013-01-28T07:50:53.167 回答