Javascript 清除主干模型/集合内存泄漏
试图通过使用Chrome中的堆快照工具来处理主干的内存管理。视图上有一个后退按钮,单击该按钮将通过主干路由器将当前视图切换为新视图 我在初始页面上拍摄快照,使用“后退”按钮导航到该页面,单击“后退”按钮并拍摄另一个快照。我希望使用比较选项时堆快照是相同的。然而,旧模型/集合似乎没有被清除(它们在快照中以白色突出显示,即仍然可以通过图的根进行访问)。此外,看起来视图本身也没有被删除(快照中的白色高亮显示相同) 我已经链接了一个图表来帮助解释()。假设视图V1嵌套了三个视图,其中V2包含模型M1,V3包含模型M2,V4包含主干集合C1。M1侦听M2的更改并相应地更新自身。为了正确移除视图V1,请执行以下操作:Javascript 清除主干模型/集合内存泄漏,javascript,backbone.js,memory-leaks,google-chrome-devtools,Javascript,Backbone.js,Memory Leaks,Google Chrome Devtools,试图通过使用Chrome中的堆快照工具来处理主干的内存管理。视图上有一个后退按钮,单击该按钮将通过主干路由器将当前视图切换为新视图 我在初始页面上拍摄快照,使用“后退”按钮导航到该页面,单击“后退”按钮并拍摄另一个快照。我希望使用比较选项时堆快照是相同的。然而,旧模型/集合似乎没有被清除(它们在快照中以白色突出显示,即仍然可以通过图的根进行访问)。此外,看起来视图本身也没有被删除(快照中的白色高亮显示相同) 我已经链接了一个图表来帮助解释()。假设视图V1嵌套了三个视图,其中V2包含模型M1,V
- 在V1及其所有子视图(以及这些子视图的子视图(如果存在))上调用Backbone.View.Remove
- 在所有模型(以及这些模型的子模型(如果存在)上调用Backbone.Events.StopListening
任何帮助都将不胜感激。谢谢。类似的问题已经有了一些答案
即使使用也可以使用此方法从内存中清除子视图和当前视图。
//FIRST EXTEND THE BACKBONE VIEW....
//Extending the backbone view...
Backbone.View.prototype.destroy_view = function()
{
//for doing something before closing.....
if (this.beforeClose) {
this.beforeClose();
}
//For destroying the related child views...
if (this.destroyChild)
{
this.destroyChild();
}
this.undelegateEvents();
$(this.el).removeData().unbind();
//Remove view from DOM
this.remove();
Backbone.View.prototype.remove.call(this);
}
//Function for destroying the child views...
Backbone.View.prototype.destroyChild = function(){
console.info("Closing the child views...");
//Remember to push the child views of a parent view using this.childViews
if(this.childViews){
var len = this.childViews.length;
for(var i=0; i<len; i++){
this.childViews[i].destroy_view();
}
}//End of if statement
} //End of destroyChild function
//Now extending the Router ..
var Test_Routers = Backbone.Router.extend({
//Always call this function before calling a route call function...
closePreviousViews: function() {
console.log("Closing the pervious in memory views...");
if (this.currentView)
this.currentView.destroy_view();
},
routes:{
"test" : "testRoute"
},
testRoute: function(){
//Always call this method before calling the route..
this.closePreviousViews();
.....
}
//Now calling the views...
$(document).ready(function(e) {
var Router = new Test_Routers();
Backbone.history.start({root: "/"});
});
//Now showing how to push child views in parent views and setting of current views...
var Test_View = Backbone.View.extend({
initialize:function(){
//Now setting the current view..
Router.currentView = this;
//If your views contains child views then first initialize...
this.childViews = [];
//Now push any child views you create in this parent view.
//It will automatically get deleted
//this.childViews.push(childView);
}
});
//首先扩展主干视图。。。。
//扩展主干视图。。。
Backbone.View.prototype.destroy_View=函数()
{
//在关门前做某事。。。。。
如果(本次关闭前){
这个。beforeClose();
}
//用于销毁相关子视图。。。
如果(这个孩子)
{
这个;
}
此.undelegateEvents();
$(this.el).removeData().unbind();
//从DOM中删除视图
这个。删除();
Backbone.View.prototype.remove.call(this);
}
//用于销毁子视图的函数。。。
Backbone.View.prototype.destroyChild=函数(){
console.info(“关闭子视图…”);
//记住使用this.childview推送父视图的子视图
如果(本.childview){
var len=this.childViews.length;
对于(var i=0;i