Ajax Web 应用程序 - 编码建议
我目前正在一个大型 ajax 密集型 Web 项目中工作,广泛使用 JQuery 和 Grails。
目前我在问自己如何使代码尽可能易于维护。因此,在您看来,问题是,最好将 JS 与 html 完全分离(并利用选择器,从而拥有干净的 HTML 代码),或者将两者混合在一起。
作为一个例子,我们假设我有一个创建评论系统的标签:
def commentTag = {
out << """
<div id="commentBox1" class="comment">
<input type="text" name="comment-something"/>
<input type="submit" value="comment"/>
</div>
<g:javascript>$('#commentBox1').commentSystem();</g:javascript>
"""
}
问题是关于最后一行。您认为将 JS 代码包含在其中是一个很好的设计理念吗?或者将其放在单独的 JS 文件中会更好吗?然后使用一些 JQuery 选择器来应用 commentSystem 行为,如下所示:
$('.comment').commentSystem();
感谢您的帮助!
问候, 尼古拉斯
I'm working at the moment in a big ajax intense Web Project using JQuery and Grails extensively.
At the moment I'm asking myself how to make the code as mantainable as possible. Thus the question would be, in your opinion, it is better to completely separate the JS from the html (and make use of Selectors and thus have clean HTML code), or mix both of them.
As an example, let's assume I have a tag which creates a comment system:
def commentTag = {
out << """
<div id="commentBox1" class="comment">
<input type="text" name="comment-something"/>
<input type="submit" value="comment"/>
</div>
<g:javascript>$('#commentBox1').commentSystem();</g:javascript>
"""
}
The question would be regarding the last line. Do you think it is a good design idea to include the JS code there, or would it be better to do it in a separate JS file. And then use some JQuery selector to apply the commentSystem behavior like this:
$('.comment').commentSystem();
Thanks for your help!
Regards,
Nicolas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IMO,最好将它们在 JS 文件中分开,但是每当您创建 TagLib 时,特定于该标签的代码都会说 commentTag 不在其他地方使用,最好仅与 commentTag 一起使用。
IMO, Its good to keep them separate in JS files, but whenever you are creating TagLib, code specific to that tag say commentTag which isn't used anywhere else, is better to be with commentTag only.