Question

I make a simple todo app:

var Todo = Backbone.Model.extend({

});

var Todos = Backbone.Collection.extend({
    model: Todo
});

var todos = new Todos();

var ItemView = Backbone.View.extend({
    tagName: "li",
    template: _.template($("#item-template").html()),
    render: function () {
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    },
    initialize: function () {
        this.listenTo(todos, 'remove', this.remove);
    },
    events: {
        "click .delete": "clear"
    },
    clear: function () {
        todos.remove(this.model);
    }
});

var AppView = Backbone.View.extend({
    el: $("body"),
    initialize: function () {
        this.listenTo(todos, 'add', this.addOne);
    },  
    addOne: function(todo) {
        var view = new ItemView({
            model: todo
        });
        this.$("#list").append(view.render().el);
    },    
    events: {
        "click #create": "create"
    },
    create: function () {
        var model = new Todo({
            title: this.$("#input").val()
        });
        todos.add(model);
    }
})

var app = new AppView();

and DEMO online is here: http://jsfiddle.net/JPL94/1/

I can add item correctly, but when I want delete some item, all of them been removed;

I found it related to the bind event in ItemView, when I click one delete button, all of them are triggered.

But how can I solve this problem?

Was it helpful?

Solution

You are listening to remove events from the collection, and if my memory serves me right a collection will dispatch a remove event whenever a model is removed, so when you remove a model from the collection, all the views will see the event.

I changed your initialize in the view to

initialize: function () {
    this.listenTo(this.model, 'remove', this.remove);
},

And it seems to work.

http://jsfiddle.net/JPL94/5/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top