Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 确认盒子问我无数次_Javascript_Jquery_Backbone.js - Fatal编程技术网

Javascript 确认盒子问我无数次

Javascript 确认盒子问我无数次,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,如果我真的确定要删除该文件,我总是被无数次地询问。我不知道为什么这个事件会多次触发 我正在使用backbone.js 这是我的观点(简称)。单击“span.delete”将启动删除功能(它工作正常) XFFView=Backbone.View.extend({ itemTpl:35;.template($(“35; xff item”).html()), 活动:{ 单击“删除”:“删除” }, 删除:函数(){ app.delFromList(此.$el.children('li')。数据('t

如果我真的确定要删除该文件,我总是被无数次地询问。我不知道为什么这个事件会多次触发

我正在使用backbone.js

这是我的观点(简称)。单击“span.delete”将启动删除功能(它工作正常)

XFFView=Backbone.View.extend({
itemTpl:35;.template($(“35; xff item”).html()),
活动:{
单击“删除”:“删除”
},
删除:函数(){
app.delFromList(此.$el.children('li')。数据('target'));
这个。$el.remove();
} 
});
这是delFromList函数,它触发确认请求。我一次又一次地被问到,直到我点击“取消”

delFromList:函数(id){
如果(确认('您确定要删除此文件吗?')){
this.collection.get(id.destroy();
}
},

我相信我发现了您的问题,正如我所料:

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();
正在侦听要删除的模型,该模型位于此行中:

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,这样就不会与删除视图混淆。

您能创建一个复制该问题的方法吗?由于backbone.js开销,几乎不可能。我需要几个小时才能把它复制出来。但这可能是因为没有先例吗?如何在delFromList函数中使用e.preventDefault而不将事件作为参数(而不是id)传递?您可以缩小它的范围以单独隔离和复制问题。喜欢我不能复制它,因为它有一个集合和一个视图,通常有一个父视图和一个我看不到的每项视图。我也看不到你正在听的哪些事件不能自己复制,哈哈。但是在替换了这个.collection.get(id).destroy()之后;对于console.debug(“test”),此循环不再出现。因此它必须与这个.collection.get(id).destroy()结合使用。然而,这些是基本的主干功能,它只是从集合中销毁obj。不确定现在要看哪里。如果删除它停止了问题,那么请查看视图,是否有类似“this.model.on('change',this.remove,this);”这样的代码?。看起来remove函数以某种方式附加到了model destroy事件上,这是一个很好的捕获,我真的没有看到这一点。谢谢!