javascript 部分 - ruby​​ on Rails

发布于 2024-10-14 16:06:46 字数 813 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Spring初心 2024-10-21 16:06:46

看看 rjs 文档...
http://api.rubyonrails.org/classes/ActionView/Helpers /PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html

您可以使用 jrails 将其与 jquery 一起使用...

然后

{render(:partial => 'content', :locals => {:content => @last_shout.content})}

您可以在关联的 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

{render(:partial => 'content', :locals => {:content => @last_shout.content})}

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 .

霞映澄塘 2024-10-21 16:06:46

我会将淡入淡出部分移动到 AjaxUpdater 中:

<script type="text/javascript">
   new Ajax.PeriodicalUpdater('content', '/shouts/update.js', { method: 'get', frequency: 1, onComplete: function() {$('content').fade({ duration: 3.0, from: 0, to: 1 });}});
</script>

这可能是一个时间问题(在调用 onComplete 之前替换文本,但更早评估文本中的脚本。)

I would move the fading part into the AjaxUpdater:

<script type="text/javascript">
   new Ajax.PeriodicalUpdater('content', '/shouts/update.js', { method: 'get', frequency: 1, onComplete: function() {$('content').fade({ duration: 3.0, from: 0, to: 1 });}});
</script>

It could be a matter of timing (the text is replaced before onComplete is called, but the script within the text is evaluated earlier.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文