Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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 Backbone.js绑定到集合“;加上「;渲染视图两次_Javascript_Backbone.js - Fatal编程技术网

Javascript Backbone.js绑定到集合“;加上「;渲染视图两次

Javascript Backbone.js绑定到集合“;加上「;渲染视图两次,javascript,backbone.js,Javascript,Backbone.js,我试图跟踪应用程序中的所有僵尸,更好地了解事件绑定是如何发生的。 我有一个视图,它将其集合“add”事件绑定到它的render函数 _.bindAll(this, "render"); this.collection.bind("add", this.render); 所以,若我在我的渲染函数中记录了一些东西,我可以在控制台中看到,在用户通过UI添加新模型之后,渲染发生了两次。控制台输出如下所示: rendering index

我试图跟踪应用程序中的所有僵尸,更好地了解事件绑定是如何发生的。 我有一个视图,它将其集合
“add”
事件绑定到它的
render
函数

_.bindAll(this, "render");
this.collection.bind("add", this.render);
所以,若我在我的渲染函数中记录了一些东西,我可以在控制台中看到,在用户通过UI添加新模型之后,渲染发生了两次。控制台输出如下所示:

rendering                                  index.js?body=1 (line 88)

POST http://localhost:3000/tasks           jquery.js?body=1 (line 8103)

rendering                                  index.js?body=1 (line 88)
var yourView = new YourDefinedView();
yourView.render(); // this is your manual call to render

//here you call to the server and when data arrives is the second render
this.collection.fetch();
我想知道为什么会这样。我知道一个事实,模型只被添加到集合中一次,所以我认为事件应该只被触发一次。然后我不明白为什么执行了两次
render

我在类似的问题中进行了研究,但它有所不同,因为我使用的是
add
事件,而不是
change

您是否实例化了视图两次?可能有两种不同的视图。

我想您调用了render两次

你正在做这样的事情:

rendering                                  index.js?body=1 (line 88)

POST http://localhost:3000/tasks           jquery.js?body=1 (line 8103)

rendering                                  index.js?body=1 (line 88)
var yourView = new YourDefinedView();
yourView.render(); // this is your manual call to render

//here you call to the server and when data arrives is the second render
this.collection.fetch();
我不认为render是在集合接收新项时绑定的最佳方法

查看此示例,了解我们如何绑定特定事件,以便将集合中的项目添加到视图中:

事实证明,通过事件聚合器,对
渲染
还有一个额外的绑定。它是由另一个开发人员添加的。

您应该向我们展示更多代码。实现“用户通过UI添加新模型”的代码是什么?