As the title, I want to abstract some function and share them between different unobtrusive javascript files. How can I do?
To be more specific, for example, I have two files: show.js.erb, create.js.erb in views/object/
When I response to ajax request, I will render them.
Now this two files share some same code, so I want to abstract them and put it to a new file.
How can I achieve this?
Like with
html.erbfiles you can also have partial forjs.erbfiles.Just extract the common code into a partial (starting with a
_, so e.g_common_code.js.erbinviews/object/).Then you can just use the render function in the
show.js.erborcreate.js.erbfiles like this to include your common code.