EJS模板中未定义JSON字段

时间:2015-03-16 21:46:29

标签: javascript json backbone.js 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方法如下所示:

  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;
    },

因此,用户var定义在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 个答案:

没有答案