2012-09-27 15 views
3

這是我想出來的代碼:http://jsfiddle.net/sbrsK/10/
它正常運行沒有的jsfiddle爲什麼瀏覽器在使用handlebars.js時在Firefox中返回錯誤「TypeError:this._input爲null」(在Chrome中類似)?

任何錯誤試圖通過Web服務器在本地運行同樣在我的電腦不能正常工作。 下列文件被加載:

  • 的index.html
  • app.js

在Firefox我得到這個錯誤:

TypeError: this._input is null @ http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js:364 

在Chrome我得到這個錯誤:

Uncaught TypeError:Cannot call method 'match' of null 
under match = this._input.match(this.rules[rules[i]]); in handlebars-1.0.0.beta.6.js 

這個link之前有一個非常類似的問題,但似乎它仍然是開放的。

所以問題是爲什麼這個錯誤發生時,它在jsfiddle中正常工作? 什麼是在本地運行的正確方法?

回答

7

該錯誤意味着#entry-template不在DOM當您嘗試使用它:

var source = $("#entry-template").html(); // There is no #entry-template in the DOM here 
var template = Handlebars.compile(source); 

這意味着,你最終想要編譯undefined作爲把手模板並不起作用。你可以看到這個錯誤:

http://jsfiddle.net/ambiguous/LprDR/

打開你的控制檯。

jsfiddle的工作原理是因爲你已經加載了DOM後運行了所有的代碼,這就是默認的jsfiddle行爲。

您的真實代碼中可能有加載順序問題,請嘗試將其包裝在$(function() { /*...*/ })包裝中。

+0

謝謝你的指導@ mu-is-too-short請檢查我的答案,並讓我知道這是你的意思。此外,呈現的頁面無法正確顯示。任何原因爲什麼? [index.html](http://pastebin.com/tKNGCSyA); [app.js](http://pastebin.com/BGPcz71w); [輸出](http://pastebin.com/5uH25H1B) – boiledice

+0

I從[here](http://icanhazjs.com/)獲得了一些額外的幫助 – boiledice

+0

我修正了我的app.js,我刪除了console.log,輸出現在如預期的那樣。 – boiledice

相關問題