Javascript – Handlebars Template is not rendered

handlebars.jshtmljavascript

Hey I am completly new to Handlebars.js and almost new to JavaScript itself.
So I tried to go through the following tut: http://coenraets.org/blog/phonegap-tutorial/

I completed Part 5 and wanted to test the app in the browser. But the page ist blank. Does the browser recognize handlebars.js by himself and renders the handlebars template automatically? Or why do I receive a blank page?

I hope the questions is not to basic, however I have no clue how to proceed or where my error lies.
index.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();

I get the follwoing errors in main.js:
ln. 15: Uncaught ReferenceError: Handlebars is not defined
ln 20. :Uncaught TypeError: Object # has no method 'homeTpl'

Kind regards,
Snafu

Best Answer

Move your handlebars.js above your main.js script tags.