忽略未定义的数据/增值经销商在下划线模板 [英] Ignoring undefined data / vars in an underscore template

查看:113
本文介绍了忽略未定义的数据/增值经销商在下划线模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在学习骨干所以多多包涵;

Still learning backbone so bear with me;

我想添加空白字段视图的新模式,但我已经创建的模板有一大堆

I'm trying to add a new model with blank fields to a view, but the template I've created has a whole bunch of

<input value="<%= some_value %>" type="whatever" />

获取数据时工作完全正常,它将填充它,一切顺利。麻烦出现时,我想创建一个新的(空)的视图,它给了我

Works perfectly fine when fetching data, it populates it and all goes well. The trouble arises when I want to create a new (blank) rendered view, it gives me

Uncaught ReferenceError: some_value is not defined

我可以设置默认(我几那些在DB的默认值已经做的),但是这意味着他们中有40空白打字;是否有处理这种更好的办法?

I can set defaults (I do already for a few that have default values in the db) but that means typing out over 40 of them with blanks; is there a better way of handling this?

我用下划线模板本身摆弄周围,试图像&LT东西;%= IF(SOME_VALUE =未定义!){SOME_VALUE}%GT; 但也似乎有点麻烦。

I'm fiddling around with the underscore template itself, trying something like <%= if(some_value != undefined){ some_value } %> but that also seems a bit cumbersome.

推荐答案

有没有实际的修复的这个因强调方法的模板实现的。

There is no actual fix for this due to the way underscore templates are implemented.

请参阅这个讨论一下:

恐怕,这仅仅是方式与(){}工作在JS。如果变量未声明,这是一个的ReferenceError。没有什么,我们可以做的,而preserving模板行为的其余部分。

I'm afraid that this is simply the way that with(){} works in JS. If the variable isn't declared, it's a ReferenceError. There's nothing we can do about it, while preserving the rest of template behavior.

你可以完成你在找什么的唯一方式是自动换行对象与另一个对象像其他答案建议,或者设置的默认值。

The only way you can accomplish what you're looking for is to either wrap the object with another object like the other answer suggested, or setting up defaults.

这篇关于忽略未定义的数据/增值经销商在下划线模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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