嘿,我对 Handlebars.js 完全陌生,对 JavaScript 本身也几乎是新手。 所以我尝试执行以下操作:http://coenraets.org/blog/phonegap-tutorial/
我已完成第 5 部分,并想在浏览器中测试该应用程序。但页面是空白的。浏览器是否会自己识别handlebars.js并自动渲染handlebars模板?或者为什么我会收到空白页?
我希望问题不是很基本,但是我不知道如何继续或我的错误在哪里。 索引.html:
<html>
<body>
<script id="home-tpl" type="text/x-handlebars-template">
<div class='header'><h1>Home</h1></div>
<div class='search-bar'><input class='search-key' type="text"/></div>
<ul class='employee-list'></ul>
</script>
<script id="employee-li-tpl" type="text/x-handlebars-template">
{{#.}}
<li><a href="#employees/{{this.id}}">{{this.firstName}} {{this.lastName}}<br/>{{this.title}}</a></li>
{{/.}}
</script>
<script src="lib/jquery-1.8.2.min.js"></script>
<script src="js/storage/memory-store.js"></script>
<script src="js/main.js"></script>
<script src="lib/handlebars.js"></script>
</body>
main.js:
var app = {
findByName: function() {
var self = this;
this.store.findByName($('.search-key').val(), function(employees) {
$('.employee-list').html(self.employeeLiTpl(employees));
});
},
initialize: function() {
var self = this;
this.store = new MemoryStore(function() {
self.renderHomeView();
});
this.homeTpl = Handlebars.compile($("#home-tpl").html());
this.employeeLiTpl = Handlebars.compile($("#employee-li-tpl").html());
},
renderHomeView: function() {
$('body').html(this.homeTpl());
$('.search-key').on('keyup', $.proxy(this.findByName, this));
},
showAlert: function (message, title) {
if (navigator.notification) {
navigator.notification.alert(message, null, title, 'OK');
} else {
alert(title ? (title + ": " + message) : message);
}
},
};
app.initialize();
我在 main.js 中收到以下错误: 英寸。 15: Uncaught ReferenceError : Handlebars 未定义 ln 20. :Uncaught TypeError: Object # has no method 'homeTpl'
亲切的问候, 困惑
最佳答案
将 handlebars.js
移至 main.js
脚本标记上方。
关于javascript - Handlebars 模板未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19908770/