【问题标题】:underscore.js:1461 Uncaught ReferenceError: users is not defined [duplicate]underscore.js:1461 Uncaught ReferenceError:用户未定义[重复]
【发布时间】:2017-02-07 11:23:37
【问题描述】:

我是backscore.js 和backscore.js 的新手。我正在尝试创建一个示例应用程序。

我的代码是:

<script type="text/template" id="user-list-template">
    <table class = "table striped">
            <thead>
                <tr>
                    <th>User Name</th>
                    <th>Age</th>
                    <th>Location</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <% _.each(users, function(user){%>
                        <tr>
                            <td><% user.get('sUserName') %></td>
                            <td><% user.get('iAge') %></td>
                            <td><% user.get('sLocation') %></td>
                            <td></td>
                        </tr>
                <% }) %>
            </tbody>
    </table>
</script>
var UserList = Backbone.View.extend({
    el: '.page',
    render: function() {
        var that = this;
        var users = new Users;
        users.fetch({
            success: function(users) {
                alert("REST Service call was success");
                var template = _.template($('#user-list-template').html(), { users: users.models });
                that.$el.html(template);
                console.log('The content rendered here...');
            }
        })

    }
});

但我遇到了这个异常:

Uncaught ReferenceError: users is not defined
    at HTMLDivElement.eval (eval at m.template (underscore.js:1454), <anonymous>:6:9)
    at HTMLDivElement.c (underscore.js:1461)
    at n.access (jquery.min.js:3)
    at n.fn.init.html (jquery.min.js:3)
    at success ((index):64)
    at Object.t.success (backbone.js:1051)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at x (jquery.min.js:4)
    at XMLHttpRequest.b (jquery.min.js:4)

【问题讨论】:

  • 添加 "=" 这样&lt;%= user.get('sUserName') %&gt; 并将that.$el.html(template); 更改为that.$el.html(template).toJSON();
  • 参见this answer,它解释了_.template 的作用以及如何使用它。
  • @EmileBergeron 他们可能正在使用带有较新下划线的旧教程。在 1.7 之前你可以_.template(tmpl, data) 但在 1.7 之后你必须使用两步过程。有一些旧教程仍然在 Google 结果 AFAIK 的顶部。
  • @muistooshort 是的,很有可能。关于这个确切的问题有很多问题,这就是为什么我没有回答这个问题。

标签: javascript backbone.js underscore.js


【解决方案1】:

正如所指出的,您应该使用 &lt;%= %&gt;(或者更好的是 &lt;%- %&gt; 被转义),但您的主要问题看起来是您调用模板的方式。

下划线中的_.template() 函数又返回一个可重用的函数,您可以使用不同的数据调用该函数。

var users = new Backbone.Collection([
	{sUserName: 'Alice', iAge: 35, sLocation: 'London'},
	{sUserName: 'Bob', iAge: 5, sLocation: 'Buenos Aires'}
]);
var template = _.template($('#user-list-template').html());
$('#result').html(template({users: users.models}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<script type="text/template" id="user-list-template">
    <table class = "table striped">
            <thead>
                <tr>
                    <th>User Name</th>
                    <th>Age</th>
                    <th>Location</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <% _.each(users, function(user){%>
                        <tr>
                            <td><%- user.get('sUserName') %></td>
                            <td><%- user.get('iAge') %></td>
                            <td><%- user.get('sLocation') %></td>
                            <td></td>
                        </tr>
                <% }) %>
            </tbody>
    </table>
</script>
<div id="result"/>

【讨论】:

  • 谢谢。它很有效,对我有很大帮助.....再次感谢。
猜你喜欢
相关资源
最近更新 更多
热门标签