我正在扩展Backbone.View以支持子视图,我有一个addSubview
方法,它只是将一个Backbone视图添加到一个散列。很简单。我现在试图让它如此,只要你看到它破坏所有的子视图。我认为这将是很容易为好,但我的方法在一个无限循环会:这是为什么递归并导致无限循环?
destroy: function() {
debugger;
// Call destroy on all subviews. If the subviews have subviews they'll
// be destroyed as well
for (var v in this._subviews) {
this._subviews[v].destroy();
}
// Instead of calling `delete` on every view we wipe everything out after
// we're done destroying all the views
this._subviews = {};
// Finally, since all the subviews are destroyed it's safe to destroy
// this view
this.remove();
},
会发生什么事是,调试器被称为第一次和this
是视图destroy
被称为(右) ,第二次在第一个子视图上调用(右侧),第三次 - 它仍然继续调用第一个子视图。播放通过一步一步的:
- 点击调试
- 获取到
this._subviews[v].destroy();
线和v
== 1子视图 - 跳回到顶部调试器。
就是这样。它永远重复。任何想法或建议?
演示:http://jsbin.com/iyApuga/1/edit
你可以创建一个小提琴吗? – thefourtheye
@thefourtheye在这里你去:http://jsbin.com/iyApuga/1/edit –