javascript 部分 - ruby on Rails
我有一个使用 AJAX(原型框架)每 3 秒加载一次的部分,这样:
<script type="text/javascript">
new Ajax.PeriodicalUpdater('content', '/shouts/update.js', { method: 'get', frequency: 1});
</script>
这是加载部分的控制器的部分:
respond_to do |format|
format.js {render(:partial => 'content', :locals => {:content => @last_shout.content})}
end
这是部分“_content.html.erb”的内容:
<h2><%= content %></h2>
到目前为止一切效果很好。
我想补充一点,每次重新加载部分时,它都会使用 Scriptaculous 框架淡入淡出。 我将此代码添加到 _content.html.erb 部分:
<script type="text/javascript">
$('content').fade({ duration: 3.0, from: 0, to: 1 });
</script>
问题是,不是每次重新加载部分时都执行代码,而是只执行一次。 我需要做什么才能让 javascript 代码在每次加载部分时执行?
I have a partial that gets loaded every 3 seconds using AJAX (Prototype framework), this way:
<script type="text/javascript">
new Ajax.PeriodicalUpdater('content', '/shouts/update.js', { method: 'get', frequency: 1});
</script>
this is the part of the controller that loads the partial:
respond_to do |format|
format.js {render(:partial => 'content', :locals => {:content => @last_shout.content})}
end
this is the content of the partial "_content.html.erb" :
<h2><%= content %></h2>
So far everything works great.
I want to add, that every time the partial will be reloaded, it will be fadeIn using Scriptaculous framework.
I added this code to the _content.html.erb partial:
<script type="text/javascript">
$('content').fade({ duration: 3.0, from: 0, to: 1 });
</script>
The problem was that instead of executing the code every time the partial gets reloaded, it executed it only one time.
What do I need to do on order to make javascript code execute every time the partial gets loaded ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 rjs 文档...
http://api.rubyonrails.org/classes/ActionView/Helpers /PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html
您可以使用 jrails 将其与 jquery 一起使用...
然后
您可以在关联的 rjs 文件中完成相同的操作并添加一些其他功能。
使用起来很简单,看网上的例子。
have a look at the rjs docs ...
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html
you could use it with jquery using jrails ...
then instead of
you could accomplish the same action in the associated rjs files and add some other functions.
It's simple to use , look at some example online .
我会将淡入淡出部分移动到 AjaxUpdater 中:
这可能是一个时间问题(在调用 onComplete 之前替换文本,但更早评估文本中的脚本。)
I would move the fading part into the AjaxUpdater:
It could be a matter of timing (the text is replaced before onComplete is called, but the script within the text is evaluated earlier.)