1

我具有在我得到一個存儲器中泄漏 IE淘汰賽2.3.0的問題。我有一個顯示在屏幕上的結果列表,並且結果集具有分頁功能。有一個AJAX調用返回給控制器以獲得下一組分頁結果。當最初將第一頁綁定到屏幕時,IE的內存負載大約爲150Mb,但每頁增加大約50Mb到100Mb,但Chrome或FireFox中不會出現此問題。敲除2.3.0 IE內存泄漏

這個問題似乎是常見的,我已經看過很多例子,如: KnockoutJS Memory Leak 我想這種模式複製到無濟於事,這是我的KO腳本文件看起來像:

var householdViewModel = ko.observable(); 

var HouseholdViewModel = function (data) { 
    ko.mapping.fromJS(data, mapping, this);  
}; 

var AccountViewModel = function (data) { 
    ko.mapping.fromJS(data, mapping, this); 
}; 

var mapping = { 
    'Households': { 
     create: function(options) { 
      return new HouseholdViewModel(options.data); 
     } 
    }, 
    'Accounts': { 
     create: function (options) { 
      return new AccountViewModel(options.data); 
     } 
    } 
}; 

我使用MVC框架做我的AJAX調用它看起來像下面這樣:

@Ajax.ActionLink(">", "PageInfo", new {pageOption = "Next"}, new AjaxOptions {UpdateTargetId = "SearchResults", HttpMethod = "GET", OnBegin = "OnBeginAjaxListManipulation", OnComplete = "OnCompleteAjaxListManipulation"}, new {@class = "textNavigationElement"}) 

隨着回調JS方法,看起來像這樣:

function OnCompleteAjaxListManipulation() { 
     //ko.cleanNode(document.getElementById("SearchResults")); 
     var HHVM = { Households: ko.mapping.fromJS(@Html.Raw(Json.Encode(Model.Households))) }; 
     householdViewModel(new HouseholdViewModel(HHVM)); 
     //ko.applyBindings(householdViewModel, document.getElementById("SearchResults")); 
    } 

我想我需要指出我有一個名爲Info.cshtml的頁面和一個名爲results.cshtml的部分頁面,它將顯示搜索結果。在信息頁面中,我撥打以下電話;

var HHVM = { Households: ko.mapping.fromJS(@Html.Raw(Json.Encode(Model.Households))) }; 
    householdViewModel(new HouseholdViewModel(HHVM)); 
    ko.applyBindings(householdViewModel, document.getElementById("SearchResults")); 

搜索結果將第一頁,但在第二頁,什麼顯示器上顯示的罰款。如果我在JS回調中添加第二個applyBindings,它可以工作,但是我得到了內存泄漏問題。我無法弄清楚這一點,任何幫助將不勝感激。

喬希

回答

0

我通過手動控制可觀察的數組這可能是在你應該做的方式固定它。首先,我改變我的模式是:

var Households = ko.observableArray(HouseholdViewModel); 

var HouseholdViewModel = function (data) { 
    ko.mapping.fromJS(data, mapping, this);  
}; 

var AccountViewModel = function (data) { 
    ko.mapping.fromJS(data, mapping, this); 
}; 

var mapping = { 
    'Households': { 
     create: function(options) { 
      return new HouseholdViewModel(options.data); 
     } 
    }, 
    'Accounts': { 
     create: function (options) { 
      return new AccountViewModel(options.data); 
     } 
    } 
}; 

然後改變了阿賈克斯稱爲被觸發的方式,我刪除了UpdateTargetId並決定將這個控制自己的代碼如下所示:

@Ajax.ActionLink(">>", "PageInfo", new {pageOption = "Last"}, new AjaxOptions {HttpMethod = "GET", OnBegin = "OnBeginAjaxListManipulation", OnComplete = "OnCompleteAjaxListManipulation", OnSuccess = "BindModel"}, new {@class = "textNavigationElement"}) 

function BindModel(e) { 
     var isBound = !!ko.dataFor(document.getElementById("SearchResults")); 
     if (isBound) 
      Households.removeAll(); 

     $.each(e.Households, function (index, value) { 
      var hh = new HouseholdViewModel(value); 
      Households.push(hh); 
     }); 

     if (!isBound) 
      ko.applyBindings(Households, document.getElementById("SearchResults")); 
    } 

就是這樣!其結果是,每一個模式勢必時候就會清除觀察到的陣列和讀取項目和因爲是可觀察的數組綁定頁面,我們不需要做任何ko.CleanNode並重新綁定:-)

我希望這可以幫助別人。