jquery - Declaring a script from a PartialView only once -
i have razor partialview works metro-ui-style tiles. it's simple partialview allows me display tiles bound viewmodel
, nothing spectacular.
since it's not used in pages, don't want include jquery block on every page load. instead prefer script declared inline partialview, registered once in pagecode.
the view following (i have reenginered metrofy template)
@model ienumerable<metrostyletile> <div class="container tiles_hub"> <div class="sixteen columns alpha"> @foreach (metrostyletile tile in model) { <div class="tile_item 4 columns alpha"> <a href="@url.action(tile.action, tile.controller, routevalues: tile.mvcarea != null ? new { area = tile.mvcarea } : null)" class="tile"> @html.label(tile.description) @if (tile.imageurl != null) { @html.image(tile.imageurl.tostring(), tile.title, htmlattributes: new { @class = "tile_img" }) } </a> </div> } </div> @section scripts{ <script type="text/javascript"> $(document).ready(function ($) { //tiles hover animation $('.tile').each(function () { var $span = $(this).children('span'); $span.css('bottom', "-" + $span.outerheight() + 'px'); }); var bottom = 0; $('.tile').hover(function () { var $span = $(this).children('span'); if (!$span.data('bottom')) $span.data('bottom', $span.css('bottom')); $span.stop().animate({ 'bottom': 0 }, 250); }, function () { var $span = $(this).children('span'); $span.stop().animate({ 'bottom': $span.data('bottom') }, 250); }); }); </script> } </div>
the above code fine when use partialview once in page, if use @html.partial
twice script twice.
now let me clarify: ask question on stackoverflow learn more platform, , don't workaround solutions.
i could use javascript global variable see if function must declared again or not.
i could register script in layout make appear in pages , not worry.
i'm asking how print script response
once. learning how allows me lighten page payload when multiple partialviews developed (so client gets script when needed, , once if needed).
how can that?
Comments
Post a Comment