干掉js.erb文件(包括另一个js.erb文件)

时间:2022-11-07 08:47:48

Most of my js.erb files contains something like this at the bottom:

我的大多数js.erb文件底部都包含这样的内容:

$("#flash_message").html("<%= escape_javascript(content_tag(:p, flash[:note], :class => "note")) %>");
$("#flash_message").fadeOut(2000);
$("#loading").remove();

I would like to move these lines into a seperate file and then call that file from each of my js.erb files. Is something like that possible?

我想将这些行移动到一个单独的文件中,然后从我的每个js.erb文件中调用该文件。有可能吗?

Best regards. Asbørn Morell

最好的祝福。 AsbørnMorell

2 个解决方案

#1


7  

Yes, you may create an app/views/shared/_flash_messages.js.rjs partial, which you can then render from anywhere (e.g. from other rjs partials.)

是的,您可以创建app / views / shared / _flash_messages.js.rjs partial,然后可以从任何地方(例如从其他rjs partials)渲染。

My approach in these kinds of applications has been as follows:

我在这些应用程序中的方法如下:

  • for non-AJAX responses that may have a flash:

    对于可能有闪存的非AJAX响应:

    • in the layout (e.g. layouts/application.erb), add e.g.:
      render :partial => 'shared/flash_messages.html.erb'
    • 在布局中(例如layouts / application.erb),添加例如:render:partial =>'shared / flash_messages.html.erb'

  • for AJAX responses that may also need to display a flash message, I added the following rjs code:

    对于可能还需要显示flash消息的AJAX响应,我添加了以下rjs代码:

    • in each rjs response (e.g. controller/action.js.rjs), add e.g.:
      render :partial => 'shared/flash_messages.js.rjs'
    • 在每个rjs响应中(例如controller / action.js.rjs),添加例如:render:partial =>'shared / flash_messages.js.rjs'

Where the two partials do the necessary to render the flash, then call flash.discard(:error) or flash.discard(:notice) as appropriate.

如果两个部分执行渲染闪存所必需的操作,则根据需要调用flash.discard(:error)或flash.discard(:notice)。

Sample app/views/shared/flash_messages.html.erb file:

示例app / views / shared / flash_messages.html.erb文件:

<% if flash[:error] %>
<div id="flash_message" class="error"><%= h(flash[:error]) %></div>
<% flash.discard(:error) %>
<% elsif flash[:notice] %>
<div id="flash_message" class="notice"><%= h(flash[:notice]) %></div>
<% flash.discard(:notice) %>
<% else %>
<div id="flash_message" style="display: none;" />
<% end %>

Sample app/views/shared/flash_messages.html.rjs file:

示例app / views / shared / flash_messages.html.rjs文件:

if !flash[:error].blank?
  page['flash_message'].
    replace_html(flash[:error]).
    removeClassName('notice').
    addClassName('error').
    show()
else
  page['flash_message'].
    replace_html(flash[:notice]).
    removeClassName('error').
    addClassName('notice').
    show()
end

#2


0  

<%= render :partial => 'common' %>, perhaps?

也许是<%= render:partial =>'common'%>?

http://api.rubyonrails.org/classes/ActionController/Base.html#M000633

#1


7  

Yes, you may create an app/views/shared/_flash_messages.js.rjs partial, which you can then render from anywhere (e.g. from other rjs partials.)

是的,您可以创建app / views / shared / _flash_messages.js.rjs partial,然后可以从任何地方(例如从其他rjs partials)渲染。

My approach in these kinds of applications has been as follows:

我在这些应用程序中的方法如下:

  • for non-AJAX responses that may have a flash:

    对于可能有闪存的非AJAX响应:

    • in the layout (e.g. layouts/application.erb), add e.g.:
      render :partial => 'shared/flash_messages.html.erb'
    • 在布局中(例如layouts / application.erb),添加例如:render:partial =>'shared / flash_messages.html.erb'

  • for AJAX responses that may also need to display a flash message, I added the following rjs code:

    对于可能还需要显示flash消息的AJAX响应,我添加了以下rjs代码:

    • in each rjs response (e.g. controller/action.js.rjs), add e.g.:
      render :partial => 'shared/flash_messages.js.rjs'
    • 在每个rjs响应中(例如controller / action.js.rjs),添加例如:render:partial =>'shared / flash_messages.js.rjs'

Where the two partials do the necessary to render the flash, then call flash.discard(:error) or flash.discard(:notice) as appropriate.

如果两个部分执行渲染闪存所必需的操作,则根据需要调用flash.discard(:error)或flash.discard(:notice)。

Sample app/views/shared/flash_messages.html.erb file:

示例app / views / shared / flash_messages.html.erb文件:

<% if flash[:error] %>
<div id="flash_message" class="error"><%= h(flash[:error]) %></div>
<% flash.discard(:error) %>
<% elsif flash[:notice] %>
<div id="flash_message" class="notice"><%= h(flash[:notice]) %></div>
<% flash.discard(:notice) %>
<% else %>
<div id="flash_message" style="display: none;" />
<% end %>

Sample app/views/shared/flash_messages.html.rjs file:

示例app / views / shared / flash_messages.html.rjs文件:

if !flash[:error].blank?
  page['flash_message'].
    replace_html(flash[:error]).
    removeClassName('notice').
    addClassName('error').
    show()
else
  page['flash_message'].
    replace_html(flash[:notice]).
    removeClassName('error').
    addClassName('notice').
    show()
end

#2


0  

<%= render :partial => 'common' %>, perhaps?

也许是<%= render:partial =>'common'%>?

http://api.rubyonrails.org/classes/ActionController/Base.html#M000633