未捕获的引用错误: 未定义导出 ID

Uncaught ReferenceError: exportid is not defined

本文关键字:未定义 ID 错误 引用      更新时间:2023-09-26

好吧,我创建了一个事件,以便选中许多复选框,如果它们是手动的,它应该将这些复选框分配给受让人,但如果不是,它们应该是。这是视图函数,

assignUserClicked : function() {
    var self = this;
    var modal = new AssignmentUserModalView({
            collection: this.getSelected()
        }).open();
    this.model.save({
        integrationlevel: 'manual'
    }, {
        type: 'POST',
        wait: true
    });
    var self = this;
    var refresh = function (model) {};
    this.listenTo(modal, 'ok', function () {
        self.model.fetch({
            data: {
                exportid: self.model.id
            }
        });
    });
    modal.open();
},

TMPL在下面,

<div class="btn-toolbar pull-right">
    <div class="btn-group" title="Bulk Actions">
    <button class="btn dropdown-toggle btn-primary" data-toggle="dropdown">
        <span class="glyphicon glyphicon-th-list"></span>
        Bulk Actions
        <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
        <h3 class="pull-left no-margin">
            <%= name %>
            <small>-
                <%= exportid %></small><% if (integrationlevel== 'manual') { %>
            <small>-</small>
            <small class="text-danger">NON CHANGEABLE</small><% } %>
            <%= exportid %>
        </h3>
        <li>
            <% if (integrationlevel == 'automatic'|| integrationlevel== 'Automatic'){ %>
            <a href="javascript:void(0);" id="ud_changetomanual" tabindex="-1">
                <span class="glyphicon glyphicon-ok"></span>
                Set to manual
            </a>
            <% } else { %>
            <a href="javascript:void(0);" id="ud_assignee" tabindex="-1">
                <span class="glyphicon glyphicon-user"></span>
                Change Assignment
            </a>
            <% } %>
        </li>
    </ul>
</div>

它给了我一个错误,说导出ID未定义,一个想法?

这个:

self.model.fetch({
    data: {
        exportid: self.model.id
    }
});

{exportid: self.model.id}作为 AJAX 请求的一部分发送到服务器,它不会将exportid作为属性添加到模型中。然后稍后你正在做这样的事情:

var t = _.template(...);
var h = t(this.model.toJSON());

但是this.model.toJSON()不会有exportid,因为没有人添加它;然后模板函数会在你<%= exportid %>时尝试找到exportid,但由于它不存在,你会得到一个ReferenceError

您需要exportid模型的属性或仅进入模板数据。我不知道什么方法适合你的情况。


请记住,_.template 将模板转换为 JavaScript 函数,并且该函数使用 with 使其参数对象的键看起来像变量。所以当你说:

var data = this.model.toJSON();
t(data);

对于包含以下内容的模板

<%= exportid %>

JavaScript引擎将查找data.exportid,然后,如果它在data中找不到它,它会查找一个名为exportid的范围内变量。第二次查找会导致ReferenceError,而不是nullundefined问题。