Pergunta

I am looping an array in an underscore template and what to add onclick for each of the elements. Can I somehow pass the object directly to a function in the underscore template?

e.g. here I'm trying to pass the client element to the editClient() function

<table>
    <tbody>
        <% _.each(clients, function(client) { %>
        <tr>
            <td><a href="#" onclick="<% editClient(client); %> return false;"><%= client.name %></a></td>
        <% }); %>
    </tbody>
</table>

My guess is that underscore just parses everything to a string and renders the result, meaning I would need to instead pass a id to the editClient function and fetch the client using that.

Foi útil?

Solução

Since this is just rendering to html and not actually directly binding the onclick event the id should be passed instead.

e.g.

<td><a href="#" onclick="editClient(<%= client.id %>); return false;"><%= client.Name %></a></td>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top