javascript - Handlebars 模板未渲染

标签 javascript html handlebars.js

嘿,我对 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/

相关文章:

javascript - 这个JS block 怎么会人手不足呢? (JS 复习)

javascript - getBoundingClientRect 不存在

html - 需要帮助集中我的 cta

html - 基本 HTML/CSS - 边距和显示行为

templates - Handlebar.js 预编译模板;这是什么意思?

javascript - 如何在 html 中使用 Handlebars

javascript - 如何防止 js 文件下载并返回传输到 c# webBrowser?

javascript - jQuery Accordion 问题在这里

javascript - 如何使用 jquery 删除 CSS 属性

javascript - Patternlab Handlebars 将数据变量作为字符串文字传递