为什么 Rails 辅助方法会阻止“不引人注目的 JavaScript”?
我听 Ryan Bates 说 Rails JavaScript 帮助器方法可以防止“不引人注目的 JavaScript”。
这是正确的吗?如果是,有人可以解释为什么吗?
I heard Ryan Bates say that the Rails JavaScript helper methods prevent "unobtrusive JavaScript".
Is this correct and, if so, could someone explain why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Unobtrusive 的意思是“不要将 HTML 与 javascript 行为混合在一起”。另一种说法是“不要仅仅因为想要使用 javascript 就更改 HTML”。 Rails javascript 帮助器方法以一种隐藏的方式执行此操作。
假设您根本没有使用 javascript。如果您想要一个 HTML 表单,您可以使用
form_for
并拥有一个常规表单。现在假设您要添加 javascript,以便您的表单提交 AJAX 请求而不是常规 HttpRequest。您有两种方法可以做到这一点。remote_form_for
第一种方法是强迫性的。您正在更改标记(查看生成的代码)。第二种方法不引人注目。通过使用 jQuery 并附加 javascript 的行为,您根本不会改变 HTML。
Unobtrusive just means "Don't mix your HTML with your javascript behavior". Another way to say this is "Don't change your HTML just because you want to use javascript". The rails javascript helper methods do just that in a kind of hidden way.
Say you weren't using javascript at all. If you wanted an HTML form you'd use
form_for
and have a regular form. Now say you want to add javascript so that your form submits an AJAX request instead of a regular HttpRequest. You have two ways to do this.remote_form_for
The first method is obtrusive. You're changing your markup (look at the generated code). The second method is unobtrusive. By using jQuery and attaching the behavior from javascript you don't alter your HTML at all.
Rails 助手将 onclick 事件直接添加到 html 中。我一直认为不显眼意味着当用户禁用 javascript 时你的页面仍然可以工作,但是在我打字时杰森·潘尼恩的答案弹出后,我检查了它。
事实上,将 javascript 与 html 完全分离的技术已被标记为不引人注目的 javascript。
the rails helpers add the onclick events right into the html. i always thought unobtrusive meant that your page will still work when a user has javascript disabled, but after jason punyon's answer popped up while i was typing, i checked into it.
indeed the technique to completely separate javascript from the html has been labeled unobtrusive javascript.