I am currently exploring backbone.js and underscore.js and working on building a sample application.
Here is the code snippet I have been using:
<script type="text/template" id="user-list-template">
<table class = "table striped">
<thead>
<tr>
<th>User Name</th>
<th>Age</th>
<th>Location</th>
<th></th>
</tr>
</thead>
<tbody>
<% _.each(users, function(user){%>
<tr>
<td><% user.get('sUserName') %></td>
<td><% user.get('iAge') %></td>
<td><% user.get('sLocation') %></td>
<td></td>
</tr>
<% }) %>
</tbody>
</table>
</script>
var UserList = Backbone.View.extend({
el: '.page',
render: function() {
var that = this;
var users = new Users;
users.fetch({
success: function(users) {
alert("Successfully called REST Service");
var template = _.template($('#user-list-template').html(), { users: users.models });
that.$el.html(template);
console.log('Content has been rendered successfully.');
}
})
}
});
However, I encountered an exception:
Uncaught ReferenceError: users is not defined
at HTMLDivElement.eval (eval at m.template (underscore.js:1454), <anonymous>:6:9)
at HTMLDivElement.c (underscore.js:1461)
at n.access (jquery.min.js:3)
at n.fn.init.html (jquery.min.js:3)
at success ((index):64)
at Object.t.success (backbone.js:1051)
at j (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at x (jquery.min.js:4)
at XMLHttpRequest.b (jquery.min.js:4)