Поле JSON не определено в шаблоне EJS

На моем интерфейсе у меня есть этот HTML:

<a name="user" id="user" value="<%=user%>"></a>

Я делаю это, чтобы быть полностью уверенным, что пользователь определен в моем интерфейсе

И тогда у меня есть файл JS с этим:

    var user;
    var teamCollection = new TeamCollection([]);  //new Backbone collection  

    $(document).ready(function(){
        user = $("#user").attr("value"); //pull user object from html
        window.userHomeMainTableView = new UserHomeMainTableView({el: $("#user-home-main-table-div")});
        window.userHomeMainTableView.render();
    });

метод визуализации userHomeMainTableView выглядит следующим образом:

  render: function () {
        console.log('teamCollection models:', teamCollection.models); //defined
        console.log('user:', user); // defined
        var data = {teamCollection:teamCollection,user:user}; //this should point to the user variable at the top of the JS file.
        var html = new EJS({url: '/js/backbone/views/UserHomeViews/templates/main_table_template.ejs'}).render(data);
        this.$el.html(html);
        return this;
    },

Итак, пользовательская переменная определена в верхней части файла JS, но каким-то образом, когда я передаю данные в шаблон EJS, команда teamCollection определяется, а пользователь - нет.

Как это может быть? Любая причина, почему это будет?

Вот файл EJS

<table class="table">
        <thead>
        <tr>
            <th>#</th>
            <th>Team Name</th>
            <th>Club</th>
            <th>Sport</th>
            <th>Delete?</th>
        </tr>
        </thead>
        <tbody>

        <%
        for(var i=0; i <teamCollection.models.length; i++) { //teamCollection IS defined :) good

        var team = teamCollection.models[i].toJSON();
           var user_id = user._id; //USER IS UNDEFINED !! :(
        %>
        <tr>
            <td>
                <%=i+1%>
            </td>
            <td>
                <a class="font-big" href='/users/<%=user_id%>/teams/<%= team._id%>/teamDashboard'>
                    <%= team.sport %>
                </a>
            </td>
            <td>
                <a class="btn btn-warning" onclick=window.userHomeMainTableView.deleteTeam("<%= team._id%>");>delete</a>
            </td>
        </tr>
        <% } %>
        </tbody>
    </table>

0 ответов

Другие вопросы по тегам