views:

85

answers:

6

I have a partial view called '_comment.erb', and it may be called by parent many times(e.g. in a loop). The '_comment.erb' looks like:

<script>
function aaa() {}
</script>

<%= comment.content %>
<%=link_to_function 'Do', 'aaa()' %>

You can see if the '_comment.erb' be called many times, that the javascript function 'aaa' will be re-defined many times. I hope it can be define only once, but I don't want to move it to parent view.

I hope there is a method, say 'run_once', and I can use it like this:

<%= run_once do %>
  <script>
  function aaa() {}
  </script>
<% end %>

<%= comment.content %>
<%=link_to_function 'Do', 'aaa()' %>

No matter how many time I call the '_comment.erb', the code inside 'run_once' will be run only once. What shall I do?

+3  A: 
<% if !@once_flag && @once_flag=1 %>
  <script>
  function aaa() {}
  </script>
<% end %>
zed_0xff
@zed, thanks, your code can solve my problem. But there is a small problem: I need to take care of the name of the instance variable "once_flag", otherwise it may break other code.
Freewind
Hi Freewind You can set the @once_flag parameter as a partial parameter. Something like , You can create a counter when starting the loop and pass the initial count (as 1 in the above case), then the rest will be != 1 and this code will workand You might keep 1 as the default parameter so that when you are calling it from another place you don't need to do extra workcheerssameera
sameera207
@sameera207, your solution work too, thanks. But I still hope there is a common helper method like "run_once" I mentioned, I don't need to care anything, just use it. Is it possible?
Freewind
A: 

Test to see if the function has already been defined. If not, define/run it

   <script type="text/javascript">
   /*<![CDATA[*/
      if (!aaa) {
        function aaa() {}
      }
   /*]]>*/
    </script>
BryanH
@BryanH, this is just a sample. Actually, the code may be javascript or ruby code, even html. I hope there is a helper method like "run_once" can do this for me.
Freewind
A: 

Hi Freewind

Try something like this

In your layout create a hidden textfield called 'flag' and make it default value as 0

(make sure you dont include this in your partial)

in your partial script

check that value and if it is != 1 execute your function

This is not a solid solution, but since your logic needs some flag to trace the number of executions this will be a workaround

cheers, sameera

sameera207
A: 

Hmm, if you want to define stuff once, You can just define another partail.

Define javascript in and render _comment.erb partial multiple times here. It should take care of both issues.

Rishav Rastogi
+3  A: 
elektronaut
@elektronaut, thanks, this the best solution
Freewind
Freewind
A: 

As far as I know, there no function like "run_once" available so you either have to write it yourself or handle your code another way. For javascript just define it in your application.js file and then you can call it whenever you want. If it is ruby code then put it in your application helpers and for HTML it really should be a partial.

sosborn