I use ASP.NET MVC (without the Razor engine). I have an EditorTemplate which requires a script to work properly. I could just put the script within the user control, but I do not want this script to be defined multiple times when the EditorTemplate is present multiple times in a page.
I also don't want to put the script in the page because that would be a dependency the consumer (the page) cannot easily know about. It's a recipe for bugs in the long run.
I think the solution would be to put this code in the ascx :
<% if (RequestState.IsEditorScriptRendered == null) { %>
RequestState.IsEditorScriptRendered = true;
<script>...</script>
<% } %>
Unfortunately, there doesn't seem to be any state with a request lifetime. How could I store the "IsEditorScriptRendered" information? Is there better way of achieving my desired result?
Pre-bundles I used my own answer to How to render a Section in a Partial View in MVC3 that was specific to scripts.
With bundles, it's cheaper to just include all my editor template JS files in a single bundle.