0
votes

I am using Underscore to create a template. I pass the models of a collection to the template, in this case a list of 'phone' objects.

Inside of the template, I define a function that, when called, creates a table row that includes a select that contains the phone numbers that can be chosen.

I have tried 'var phone = <%= phones %> inside the function. Didn't work.

Say the function is called 'create_select'. I have tried 'create_select(<%=phones %>) {}'. That didn't work either.

There might be other ways of doing this (for example, a create_select event handled in the Backbone View linked to this template) but I would like to understand, if this is not possible, why not.

1

1 Answers

0
votes

You have to call it inside your script let tag .For accessing the value and pass it to a function.

Sample js fiddle for function inside a template

<% var x = people; disp(x); %>
<% function disp(x) { %>
     <% _.each(x,  function(name) { %> 
     <li><%= name %></li> 
     <% }); %>
<% } %> 

In the sample code I am passing a json object and inside the template I have a function called disp(). I'm passing the value to the function and parsing it using _.each() function .