Search code examples
backbone.jsunderscore.jsunderscore.js-templating

Underscore templating not working for simple message with Backbone.js


Just trying to make Backbone.js display a simple message on index.html...It fails if I do try with underscore but it will append a message to the quiz_question div element if I try to do something like

questionTemplate: _.template( '<div>Hello <%= msg %></div>')

...What am I missing?

Index.html

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="quiz_question">
  <input id="back_id" type="button" value="Back">
  <input id="next_id" type="button" value="Next">
</div>
<script type="text/template" id="qtemplate"></script>
<script src="js/jquery-2.0.2.min.js"></script>
<script src="js/underscore-min.js"></script>
<script src="js/backbone-min.js"></script>
<script src="js/backbone.localStorage.js"></script>
<script src="js/questionmodel.js"></script>
<script src="js/questioncollection.js"></script>
<script src="js/questionview.js"></script>
<script src="js/app.js"></script>
<script type="text/template" id="qtemplate">
  <div><%= msg %></div>
</script>
</body>
</html>

app.js

  var app = app || {};
  $(function() {
    // Kick things off by creating the **App**.
    new app.QuestionView();
  });

questionview.js

var app = app || {};
app.QuestionView = Backbone.View.extend({
  // Instead of generating a new element, bind to the existing skeleton of
  // the App already present in the HTML.
    el: '#quiz_question',
    // Our template for the line of statistics at the bottom of the app.
    questionTemplate: _.template( $('#qtemplate').html() ),
    //questionTemplate: _.template( '<div>Hello <%= msg %></div>'),
    // Delegated events for displaying new questions, and clearing existing ones
    events: {
      'click #back_id': 'displayPreviousQuestion',
      'click #next_id': 'displayNextQuestion'
    },
    // The QuestionView listens for changes to its model, re-rendering. Since there's
    // a one-to-one correspondence between a **Question** and a **QuestionView** in this
    // app, we set a direct reference on the model for convenience.
    initialize: function() {
      //app.Questions.fetch();
      this.render();
    },
    render: function(){
        // render the function using substituting the varible 'who' for 'world!'. 
        this.$el.append(this.questionTemplate({msg: "hope floats"}));
        //***Try putting your name instead of world.
    },
    displayPreviousQuestion: function() {
    },
    displayNextQuestion: function() {
    }
});

Solution

  • Your page looks like this:

    <script src="js/questionview.js"></script>
    <!-- ... -->
    <script type="text/template" id="qtemplate">
      <div><%= msg %></div>
    </script>
    

    so questionview.js will be loaded and executed before #qtemplate is in the DOM. Inside questionview.js you have this:

    app.QuestionView = Backbone.View.extend({
      //...
      questionTemplate: _.template( $('#qtemplate').html() ),
    

    so _.template( $('#qtemplate').html() ) will be executed while questionview.js is being loaded and that happens before there is a #qtemplate available. The result is that you end up doing _.template(undefined) and that doesn't do anything useful.

    You can wrap the view definition in a $(function() { ... }) to delay its execution until after the DOM is ready or you could delay creating the template function until you need it with something like this:

    initialize: function() {
      this.questionTemplate = _.template($('#qtemplate').html());
    }
    

    in your QuestionView. There are variations on those two basic approaches but that should get you started.