Rails 3 自定义 JavaScript 事件在哪里定义?
当我查看 Rails 3 jquery-ujs 代码时,我注意到它绑定到自定义 JavaScript 事件(submit.rails、click.rails 等)。有谁知道这些自定义“.rails”事件在哪里定义?我只是想更好地理解 Rails 3 中的 UJS 东西是如何工作的,这样我就可以更有效地使用它......
As I look through the Rails 3 jquery-ujs code, I notice that it binds to custom JavaScript events (submit.rails, click.rails, etc). Does anyone know where are these custom '.rails' events defined? I'm just trying to better understand how the UJS stuff in Rails 3 works so I can use it more effectively...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些是命名空间事件。它们没有定义;
click.rails
与click
相同,但由于它的命名空间为rails
,因此您可以取消绑定或触发特定于 Rails 的事件处理程序,而无需调用元素上的所有click
事件。例如,假设您有一些元素
,并且rails.js 绑定
在您的代码中,您还拥有
: Rails 可能希望在某个时刻调用该远程处理程序,但如果它只是调用
$(this).click()
,那么它将调用该项目上的所有单击处理程序,包括您的自定义处理程序,其中可能会产生不良行为。相反,它可以调用$(this).trigger('click.rails')
,并且仅运行它定义的点击处理程序。These are namespaced events. There's no definition for them;
click.rails
is the same asclick
, but because it's namespaced withrails
, you can unbind or trigger the Rails-specific event handlers without invoking all of theclick
events on an element.For example, assume that you have some element,
<div class='foo' data-remote='true'>
, and rails.js bindsIn your code, you also have:
Now, Rails may want to invoke that remote handler at some point, but if it just called
$(this).click()
, then it would invoke all click handlers on the item, including your custom one, which might produce undesired behavior. Instead, it can call$(this).trigger('click.rails')
, and only the click handler it defined would be run.