1

ビューが不要になったときに、ビューを破棄するか、すべてのイベントのバインドを解除しようとしています。私がしていることは:

view.$el.removeData().unbind(); 
view.undelegateEvents();
view.remove();

ビューが破棄され、DOM内の関連する要素が表示されなくなりますが、イベントは存続しているようです。Chrome開発者ツールを使用してメモリ使用量を調べていますが、ビューをレンダリングして破棄するたびに、イベントリスナーが1つずつ増えています。

次のようにして、ビューイベントを出力しようとしました。

this.$el.data("events"); 

しかし、これは私に未定義を与えます。

何かご意見は?

ありがとうございました。

4

1 に答える 1

2

この優れた記事では、バックボーンでのメモリ リークを防ぐ方法について説明しています: http://andrewhenderson.me/tutorial/how-to-detect-backbone-memory-leaks/

ビューを破棄する彼の完全なルーチンは次のようになります。

this.unbind(); // Unbind all local event bindings
this.model.unbind( 'change', this.render, this ); // Unbind reference to the model
this.options.parent.unbind( 'close:all', this.close, this ); // Unbind reference to the parent view

this.remove(); // Remove view from DOM

delete this.$el; // Delete the jQuery wrapped object variable
delete this.el; // Delete the variable reference to this node
于 2013-04-05T20:49:03.980 に答える