使用 Jammit gem for Rails 编译 javascript 模板?
我们在 Rails (3.0.3 / 1.9) 上使用 Jammit 进行静态压缩以及它提供的所有其他前端功能。 我正在尝试使用 Javscript 模板来替换一些丑陋的前端代码,但看起来默认的 JST 编译器无法处理 Windows 换行符:
assets.yml 文件:
embed_assets: on
javascripts:
plugins:
- app/views/**/*.jst
当我将 _topicPreview.jst 全部放在一行上时,它编译正常:
<div class='TopicPreview <%= typeName %>'><div class='Title'> <%= summary %> </div><div class='Meta'> <span class='LastUpdate'> <%= updatedAt %> </span> <span class='PostCount'> | <%= postsCount %> Posts | </span> <span class='LikeCount'> <%= likesCount %> Likes </span> </div></div>
页面加载,并且我有 JST._topicPreview([JSON 版本的主题]) 方法可用。美丽的。
JST 的一半吸引力在于可读性/可维护性,所以我希望我的 JST 具有正确的格式。一旦我在 JST 文件中添加换行符,页面就会在 window.JST['_topicPreview'] = template('...[转义的 JST) 中抛出
Uncaught SyntaxError: Unexpected Token ILLEGAL
file]...')
在代码跟踪中,我可以看到它解析并转义单引号,所以我很惊讶它无法处理换行符。
还有其他人遇到过这个问题吗? 有没有比将一些额外的字符串替换到模板生成代码中更干净的修复方法?
元: 我对 javascript 模板还很陌生,但是它们应该有自己的 StackOverflow 标签吗?
编辑
看起来这是一个已知问题,有待修复并合并到 gem 中。
We're on Rails (3.0.3 / 1.9) using Jammit for static compression and all the other front-end goodies it provides.
I'm trying to work Javscript templates in to replace some ugly front-end code, but it looks like the default JST compiler can't handle Windows line breaks:
assets.yml file:
embed_assets: on
javascripts:
plugins:
- app/views/**/*.jst
When I have _topicPreview.jst all on one line it compiles fine:
<div class='TopicPreview <%= typeName %>'><div class='Title'> <%= summary %> </div><div class='Meta'> <span class='LastUpdate'> <%= updatedAt %> </span> <span class='PostCount'> | <%= postsCount %> Posts | </span> <span class='LikeCount'> <%= likesCount %> Likes </span> </div></div>
The page loads and I have the JST._topicPreview([JSON version of topic]) method available. Beautiful.
Half the appeal of JST is the readability / maintainability though, so I would like my JST to have proper formatting. As soon as I add line breaks into the JST file the page throws Uncaught SyntaxError: Unexpected Token ILLEGAL
in window.JST['_topicPreview'] = template('...[the escaped JST file]...')
In the code trace I can see that it parses and escapes single quotes, so I was surprised it couldn't handle the line breaks.
Has anyone else encountered this problem?
Is there a cleaner fix than hacking some extra string replaces into the template generation code?
Meta:
I'm pretty new to javascript templates, but should they have their own StackOverflow tag?
Edit
Looks like this is a known issue with a pending fix to be merged into the gem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修复已添加到 gem。 Windows 开发人员很高兴。
Fix has been added to the gem. Windows developers rejoice.