3
有什么办法来编译服务器和获得Closure编译器与生成的代码上工作Underscore.js模板?使用Closure编译器与Underscore.js _.template
的主要问题是_.template
:
_.template = function(str, data) {
var c = _.templateSettings;
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
'with(obj||{}){__p.push(\'' +
str.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(c.interpolate, function(match, code) {
return "'," + code.replace(/\\'/g, "'") + ",'";
})
.replace(c.evaluate || null, function(match, code) {
return "');" + code.replace(/\\'/g, "'")
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
})
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
+ "');}return __p.join('');";
var func = new Function('obj', tmpl);
return data ? func(data) : func;
};
生成JavaScript在它与语句。这两个明显的路线是:
- 修改Underscore.js的
_.template
不产生withs - 要挟封盖进入踢得好看
是第二个选项可能吗?
是的,我的目标是进行高级编辑。我不愿意修改_.template是因为它是定期更新的库代码 - 一个维护问题。 – hughdbrown
根据您的需求,您可能会考虑使用专用于ADVANCED编译的Closure模板。 – John