I've watched some videos on the topic of backbone js. This is an example straight from the video. It is from 2012, so I'm thinking backbone rules/library have changed, but I can't figure out why this does not work at the moment. In the video, the person shows it running in the JS Fiddle, but I can't get it to work. (I've included the necessary libraries in JS Fiddle, i.e. underscore, backbone and jQuery)
var V = Backbone.View.extend({
el:'body',
render: function () {
var data = { lat: -27, lon: 153 };
this.$el.html(_.template('<%= lat %> <%= lon%>', data));
return this;
}
});
var v = new V();
v.render();
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
This can be useful
1: If you have more then one template or sometime you are using external template so it can be useful for you inside method you can write reusable code
You used to be able to parse and fill in an Underscore template in one go like this:
But as of Underscore 1.7.0, the second argument to
_.template
contains template options:You have to compile the template using
_.template
and then execute the returned function to get your filled in template:In your case, it would look something like this: