2012-08-06 116 views
1

我想跟蹤我的應用程序中的所有殭屍,並更好地瞭解事件綁定是如何發生的。 我有一個視圖,它將其集合"add"事件綁定到它的render函數。Backbone.js綁定到集合「添加」呈現視圖兩次

_.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) 

我想知道爲什麼會發生這種情況。我知道一個事實,模型只添加一次到集合中,這讓我認爲事件只能被觸發一次。然後我不明白爲什麼render被執行了兩次。 我已經看過類似的問題here但它是不同的,因爲我使用add事件而不是change

+0

您應該向我們展示更多代碼。什麼是實現這個_「用戶通過UI添加新模型」_的代碼? – fguillen 2012-08-06 20:19:34

回答

0

原來那裏通過事件聚合器是一個額外的結合render。它是由另一個開發人員添加的。

1

你是否實例化了你的視圖兩次?這可能是兩種不同的觀點。

1

我認爲你要調用渲染兩次。

同比都在做這樣的事情:

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(); 

我不認爲這是呈現在集合收到新的項目綁定的最佳方法。

我們如何綁定從Collection中添加項目到查看特定事件,勾選這個例子: http://danielarandaochoa.com/backboneexamples/blog/2012/02/22/real-world-usage-of-a-backbone-collection/