确认盒问我无尽的时间

时间:2015-01-18 16:08:52

标签: javascript jquery backbone.js

如果我确实确定要删除该文件,我总是不停地问。我不知道为什么这个事件会多次发生。

我正在使用backbone.js。

这是我的观点(缩短)。点击" span.delete"它会触发删除功能(它完美无缺地工作)。

XFFView = Backbone.View.extend({
        itemTpl: _.template($("#xff-item").html()),
        events: {
            "click span.delete" : "remove"
        },
        remove: function() {
                 app.delFromList(this.$el.children('li').data('target'));
                 this.$el.remove();
        } 
});

这是delFromList函数,它触发了确认请求。我一次又一次地被问到,直到我点击'取消'。

delFromList: function(id) {
        if (confirm('Are you sure you want to delete this file?')) {
                this.collection.get(id).destroy();
        }
},

1 个答案:

答案 0 :(得分:0)

我相信我找到了你的问题,就像我预期的那样:

XFFView = Backbone.View.extend({
            itemTpl: _.template($("#xff-item").html()),
            events: {
                    "click span.delete" : "remove",
                    "click span.abort" : "abort"
            },
            initialize: function() {
                    this.listenTo(this.model, "change", this.render);
                    this.listenTo(this.model, "destroy", this.remove);
            },
            render: function() {
                    this.$el.html(this.itemTpl(this.model.toJSON()));
                    return this;
            },
            remove: function() {
                    app.delXFFFromList(this.$el.children('li').children('span.delete.badge').data('target'));
                    this.$el.remove();
            }

据我所知,这一行:

this.listenTo(this.model, "destroy", this.remove);

正在侦听要删除的模型,它在这一行中:

this.collection.get(id).destroy();

然后将再次调用此代码:

remove: function() {
       app.delXFFFromList(this.$el.children('li').children('span.delete.badge').data('target'));
       this.$el.remove();
}

基本上将代码抛入无限循环,或直到您的集合清空。

更改此行

this.listenTo(this.model, "destroy", this.remove);

调用this.destroy,或者将remove方法重命名为removeEntry,以免与删除视图混淆。