0

I'm using embedded javascript and defined certain functions within to build up a table:

<% function createCollectionTableRow(col) { %>
<tr>
    <td>
        <a href="/<%= database %>/collections/<%= collections[col][0].name %>"><%= collections[col][0].name %></a></td><td><%= collections[col][0].count %>
    </td>
    <td>
        <button id="<%= collections[col][0].name %>_rename_button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#renameModal">Rename</button>
        <script type="text/javascript">
            $('#<%= collections[col][0].name %>_rename_button').on('click', function() {
                $('#rename_button').on('click', function() {
                    var newname = $('#rename_input').val();
                    $('#rename').attr('action', '/collections/<%= collections[col][0].name %>/rename?newname=' + newname);
                });
            });
        </script>
    </td>
    <td>
        <button id="<%= collections[col][0].name %>_copy_button" class="btn btn-sm btn-info" data-toggle="modal" data-target="#copyModal">Copy</button>
        <script type="text/javascript">
            $('#<%= collections[col][0].name %>_copy_button').on('click', function() {
                $('#copy_button').on('click', function() {
                    var selection = $('#select_collection').val();
                    var db = $('#select_database').val();
                    $('#copy').attr('action', '/collections/<%= collections[col][0].name %>/copy?db=' + db + '&copyto=' + selection);
                });
            })
        </script>
    </td>
    <td>
        <form action="/collections/<%= collections[col][0].name %>?_method=DELETE" method="POST">
            <button class="btn btn-sm btn-danger">Delete</button>
        </form>
    </td>
    <td>
        <button id="<%= collections[col][0].name %>_truncate_button" class="btn btn-sm btn-warning" data-toggle="modal" data-target="#docTruncateModal">Truncate</button>
        <script type="text/javascript">
            $('#<%= collections[col][0].name %>_truncate_button').on('click', function() {
                $('#doc_truncate').attr('action', '/collections/<%= collections[col][0].name %>/truncate');
                $('#full_truncate').attr('action', '/collections/<%= collections[col][0].name %>/full_truncate');
            });
        </script>
    </td>
</tr>

Now I might want to reuse said function across different pages but whenever I try to move it to a seperate file, say 'functions.ejs', and use the following statement at the start of each page <% include functions.ejs %> I get told there's no such function. What gives?

Omnia87
  • 103
  • 7

1 Answers1

0

According to the README you have to include it like this:

<% include functions %>

The .ejs is not necessary

  • Not necessary but also not relevant, it doesn't make a different. I still get the create 'createCollectionTableRow is not defined' error. – Omnia87 Mar 19 '19 at 11:52
  • have you export your function in `functions.ejs` ? `module.exports={createCollectionTableRow}` – Sébastien Bousquet Mar 19 '19 at 12:29
  • Does that work with ejs though? All the literature I find on the topic just uses straight javascript, I wouldn't know how to export functions that include all that html and those <% tags. – Omnia87 Mar 19 '19 at 13:17