2012-07-05 85 views
1

我想知道是否有任何人經歷過使用Handlebar.js編譯函數的內存泄漏。Handlebar.js IE9中的內存泄漏

我目前正在研究一個單頁面應用程序,通過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); 
} 

如果我註釋掉 「變種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: "[email protected]" }, 
       { username: "allison", firstName: "Allison", lastName: "House", email: "[email protected]" }, 
       { username: "ryan", firstName: "Ryan", lastName: "Carson", email: "[email protected]" } 
      ] 
    }; 

    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的內存只是不斷攀升不降。有人可以看看這個。

非常感謝。

乾杯

回答

1

剛剛情況下,任何一個是有同樣的問題。

我有種解決了這個問題。最後我根本沒有使用車把。我切換到了MVC4包的一部分KnockOut.js。

KnockOut與IE很好地工作,但不是KnockOut的Mapping插件(一個幫助您映射JavaScript對象的插件),所以我必須手動綁定對象的每個字段。這不是太多額外的工作。我很高興使用KnockOut.js解決了內存泄漏問題。

希望Handlebar社區能夠解決未來的內存泄漏問題。