ruby on rails - How to run some code only once in view -


I have a partial view called '_comment.erb', and it can be called by parents many times ( For example in a loop). '_comment.erb' looks like this:

  & lt; Script & gt; Function aaa () {} & lt; / Script & gt; & Lt;% = comment.content% & gt; & Lt;% = link_to_function 'two', 'aa ()'% & gt;  

You can see that '_comment.erb' is sometimes called, then JavaScript 'AA' function will be redefined many times. I hope it can only be defined once, but I do not want to take it in the parent's view.

I hope there is a way, say 'run-anse', and I can use it like this: / P>

   

Regardless of how many times I call '_comment.erb', the code will only run once inside 'run_ones'. This is probably not the best solution to your problem, but to make an assistant, there will not be too much trouble at once:

Assistant

  Def output_once (name, and block) @outपुट_once_blocks || = [] Unless that @output_once_blocks.include? (Name) @output_ans_blocks & lt; & Lt; Name Conset (Capture Block) Block End End  

Usage

  <% output_once: define_aaa Do & gt%; Your stuff here & lt;% end% & gt;  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -