0

我正在使用 Backbone.js 开发一个应用程序。如果我有一个按钮并按下它,则会添加一个内容模块(视图和模型)。如果我按下其他按钮,内容将被删除(视图和模型)。如果我反复按下按钮添加,内容的视图被显示并重写,但以前的模型不会被删除。如果多次按下要添加的相同按钮,如何删除内容模型?

        var tabsContents = Backbone.View.extend({
el: "body", 
events: {
    "click .addAll": "addContentAll"    
},
initialize: function() {
    _.bindAll(this); 
    this.model = new ContentCollection();
    this.model.on("add", this.contentAdded);
    this.model.on("remove", this.contentRemoved);
},          
addContentAll: function(event) {
            this.model.add({name: "all"});          
contentAdded: function(content) {
    if (content.view == null) {
        var template_name;              
        switch(content.get("name")){        
            case 'all': template_name = 'home'; break;  
        }                                       
        content.view = new ContentView({model: content,template: $.trim($("[data-template-name='"+ template_name +"'] div").html() || "Template not found!")});
        $(".box2").empty();             this.$el.find(".content").find("div.box").append(content.view.render().el);                                                 
    }       
},  
contentRemoved: function(content) {
    if (content.view != null) { 
        content.view.remove();              
    }
    content.clear();  //Clear the properties of the model               
}   
});
var ContentView = Backbone.View.extend({
    tagName: "div",
    template: null,
    events: {
          "click .Close": "removeView"
    },
    initialize: function() {
        _.bindAll(this);
        this.template = this.options.template; 
    },
    render: function() {        
        this.$el.html(Mustache.render(this.template, this.model.toJSON())); 
        return this; 
    },
    removeView: function() {
        this.model.collection.remove(this.model); //Remove the model of the coll
        }
});     
var tabsview = new tabsContents();

我也不知道如何删除模型单击按钮添加另一个模型

4

0 回答 0