[英]Handlebar.js Memory leak in IE9
我想知道是否有人使用Handlebar.js編譯功能遇到了內存泄漏。
我目前正在開發一個單頁面應用程序,該應用程序通過Ajax調用從服務器定期輪詢數據。 每次獲取新數據時,我都會重新渲染視圖。 (我將Backbone.js與handlbar.js結合使用。我了解到,當我關閉視圖或切換到其他視圖時,我需要手動釋放視圖對象,我認為不是這種情況。至少,我認為關於此主題,請參見以下鏈接: http : //lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/ )<-我有並非完全遵循該方法,但是我嘗試解除所有事件和數據的綁定,並刪除所有引用。
這是我的代碼
// 1. Remove all the old dom
// -- purge all objects
// -- remove dom
Helpers.Purge(this.el); //Purge function is copied from Douglas Crockford's blog
view.empty();
this.compileTemplate(view, viewData, this.model.get("Template"));
// 2. Append the new view
var thisDom = document.getElementsByClassName(this.className);
Helpers.Purge(thisDom);
$(thisDom).remove();
$article.append(this.el);
this.compileTemplate函數如下所示:
compileTemplate: function (view, data, templateSelector, ratio) {
var templateSource = $(templateSelector).html(),
template = Handlebars.compile(templateSource);
var el = view.html(templateResult);
}
如果我注釋掉“ var el = view.html(templateResult);” 我不會遇到內存泄漏問題。 一旦取消注釋,IE 9的內存消耗就會開始增加。 (出於測試目的,我強制視圖每0.5秒重新編譯一次模板。)
我想知道Handlbar.js中是否存在已知的內存泄漏問題,或者這是我做錯的事情。
提前非常感謝您。
干杯
新更新:
我試圖找出問題所在,並編寫了一個微型程序來測試它是否只是handlebar.js並導致IE9上的內存泄漏。
這是代碼。
(function ($) {
function render() {
var templateSource = $("#template").html();
var compileTemplate = Handlebars.compile(templateSource);
var data = {
users: [
{ username: "alan", firstName: "Alan", lastName: "Johnson", email: "alan@test.com" },
{ username: "allison", firstName: "Allison", lastName: "House", email: "allison@test.com" },
{ username: "ryan", firstName: "Ryan", lastName: "Carson", email: "ryan@test.com" }
]
};
console.log("before compiling");
var templateWithData = compileTemplate(data);
$("#content").html(templateWithData);
console.log("after compiling");
//this.el = templateWithData;
}
setInterval(render, 500);
})(jQuery);
HTML代碼在這里:
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<div id="content">
</div>
<!-- JS -->
<script id="template" type="text/x-handlebars-template">
<table>
<thead>
<th>Username</th>
<th>Real Name</th>
<th>Email</th>
</thead>
<tbody>
{{#users}}
<tr>
<td>{{username}}</td>
<td>{{firstName}} {{lastName}}</td>
<td>{{email}}</td>
</tr>
{{/users}}
</tbody>
</table>
</script>
</body>
<script src="js/lib/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="js/lib/handlebars-1.0.0.beta.6.js" type="text/javascript"></script>
<script src="js/complieTemplateWithoutBackbone.js" type="text/javascript"></script>
</html>
IE的記憶力不斷攀升,永不下降。 可以請一個看看這個。
非常感謝你。
干杯
以防萬一有人遇到同樣的問題。
我已經解決了這個問題。 最后,我根本沒有使用車把。 我切換到了MVC4軟件包的一部分KnockOut.js。
KnockOut與IE配合得很好,但不是KnockOut的Mapping插件(可幫助您映射javascript對象的插件),因此我不得不手動綁定對象的每個字段。 不需要太多額外的工作。 我很高興使用KnockOut.js解決了內存泄漏。
希望Handlebar社區能夠在將來解決內存泄漏問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.