Using data parameter of _.template to pass an array of values
Hi,
Am new to backbone.js and was trying to pass an array to the view via the data parameter of the template. I did the following:
Template:
<% _.each(names, function(name){ %>
<li> <%= name %> </li>
<% }); %>
View:
var MyView = Backbone.View.extend({
template:_.template($('#NamesListPage').html(), names : ['a', 'b', 'c']),
render:function (data) {
$(this.el).html(this.template);
}
});
This code works perfectly fine and a list with item values a, b and c come up in the screen.
However I want the array of names to be dynamically populated (maybe as part of the initialize method) and not hard-coded values. How do I achieve this? Any reference to a variable like as in ._template($('#NamesListPage').html(), names :
myArray), throws error saying myArray is not defined even if I define this array in the view.
Appreciate any help.
Thanks and Best Regards,
Sandeep