我该如何使用remove方法?

时间:2013-01-28 07:35:42

标签: backbone.js backbone-views

在我的骨干函数中,如何在添加新元素之前删除元素?,我的功能一切正常。问题是当我点击'更新'之类的时候,它正在更新元素,但仍旧版本可用。如何删除旧元素,并从更新调用中添加新元素。?

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

我的职能:

$(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;

})

1 个答案:

答案 0 :(得分: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()以提供虚假数据(您应该删除该部分)。