javascript - Is there a way to append code to an asp.mvc section -
javascript - Is there a way to append code to an asp.mvc section -
i have partials rendered part of complex page composition.
some of these partials need jquery ondocumentready goodness seed list info etc. there can many of these partials chosen during render (it's dynamic)
in _layout have section definition looks this
<script src="http://my/fav/cdn/jquery-1.5.1.min.js" type="text/javascript"></script> <script type="text/javascript"> jquery(function($) { @rendersection("ondocumentreadysection", false) }); </script>
in partials want write this
@section ondocumentreadysection{ $('#partial-1').init(); }
and have result of page render end this
<script src="http://my/fav/cdn/jquery-1.5.1.min.js" type="text/javascript"></script> <script type="text/javascript"> jquery(function($) { $('#partial-1').init(); $('#partial-2').init(); $('#partial-3').init(); $('#partial-n').init(); }); </script>
this create sure javascript @ bottom of rendered html told far more optimal.
instead of:
jquery(function($) { $('#partial-1').init(); $('#partial-2').init(); $('#partial-3').init(); $('#partial-n').init(); });
you should instead assign each of them mutual css class (even if don't define definition it) , in head:
jquery(function($) { $('.classname').init(); });
or if need be:
jquery(function($) { $('.classname').each(function(){ $(this).init(); }); });
javascript asp.net-mvc-3 sections
Comments
Post a Comment