检查下划线模板中未定义的变量 [英] checking for undefined variable in underscore template

查看:24
本文介绍了检查下划线模板中未定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模板中显示 libraryPrep 对象的模态视图,如下所示:

I show a modal view of libraryPrep objects in my template like this:

if (_.isUndefined(this.libraryPreps)) {
                this.$el.html(this.template({  }));
            } else {
                this.$el.html(this.template({ libraryPreps: this.libraryPreps.toJSON() }));
            }

当我有一个 libraryPreps 对象时,else 语句起作用.在我的模板中,我是这样使用的:

The else statement works when I have a libraryPreps object. In my template, I use it like this:

<select id="libraryPreps" >
                    <%  if (!_.isUndefined(libraryPreps)) { %>
                    <% _.each(libraryPreps, function (libraryPrep) { %>
                    <option value="<%=libraryPrep.id%>"><%= libraryPrep.name %></option>
                    <% }); %>
                    <% } %>
                </select>

当我没有 libraryPreps 对象时,我无法渲染我的模板,并且我在控制台上收到一个错误,指出 libraryPreps 未定义.我是否在模板中错误地检查了 undefined?我觉得我在主干模式视图中以相同的方式检查它,但出于某种原因,在我的实际模板中,它似乎不起作用.我的模板符号是否正确?谢谢.

When I don't have a libraryPreps object, I don't get my template to render and I get an error on the console that libraryPreps is undefined. Am I checking for undefined incorrectly in my template? I feel like I'm checking it the same way in my backbone modal view, but for some reason, in my actual template, it doesn't seem to work. Is my template notation correct? Thanks.

推荐答案

如果您将变量传递给函数,它会被求值并且会抛出错误,因为没有这样的变量.相反,在您的主干视图中,您正在访问一个始终有效的对象的 属性(如果不存在具有该名称的属性,则返回 undefined 值).

If you're passing the variable to a function, it is getting evaluated and will throw an error as there is no such variable. In your backbone view, in contrast, you're accessing a property of an object which will always work (and return the undefined value if no property with that name exists).

相反,您必须在其上使用 typeof 运算符,这甚至适用于未声明的变量(查看 变量 === 未定义与类型变量 === 未定义"JavaScript 检查变量是否存在(已定义/初始化)):

Instead, you will have to use the typeof operator on it, that will even work for undeclared variables (have a look at variable === undefined vs. typeof variable === "undefined" and JavaScript check if variable exists (is defined/initialized)):

<select id="libraryPreps"><%
    if (typeof libraryPreps !== "undefined") {
        _.each(libraryPreps, function (libraryPrep) { %>
            <option value="<%=libraryPrep.id%>"><%= libraryPrep.name %></option><%
        });
    }
%></select>

要在模板中使用 _.isUndefined,您需要在模板中明确提供该值.来自文档:

To use _.isUndefined in your template, you'd need to make the value explicitly available in the template. From the docs:

默认情况下,template 通过 with 语句将数据中的值放置在本地范围内.但是,您可以使用 variable 设置指定单个变量名称.这可以显着提高模板渲染的速度.

By default, template places the values from your data in the local scope via the with statement. However, you can specify a single variable name with the variable setting. This can significantly improve the speed at which a template is able to render.

_.template("Using 'with': <%= data.answer %>", {answer: 'no'}, {variable: 'data'});
=> "Using 'with': no"

因此,您可以像这样编写模板:

So with that, you can write templates like this:

 <% if (!_.isUndefined(data.libraryPreps)) { %> …
 <% if ("libraryPreps" in data) { %> …

这篇关于检查下划线模板中未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆