1

In my ASP.NET MVC4 web application, I will have many views in which the application functions as a single page application (SPA) within the view, but navigating to another view is done via traditional navigation. Each view/page is like its own SPA. So I want to deliver a viewmodel.js file specific for each page because I don't want to apply knockout bindings for all the views when I'm only looking at one of them.

My question is, should I create a separate bundle for each videmodel.js file, or can/should I somehow completely circumvent bundling for my view-specific scripts? What's the best way to circumvent the bundling?

I've tried simply appending

<script src="~/Scripts/app/inventory.viewmodel.js" type="text/javascript" />

to my Index.vbhtml file (my view), but it comes out above the rest of the scripts even though it's after the @Section Scripts block. I tried including it in the @Section Scripts block, but I can't figure out the right syntax to avoid using @Scripts.Render.

Once again, my question is not only how to circumvent bundling but whether circumventing bundling is the best option here.

Edit I switched from the self-closing script tag to

<script src="~/Scripts/app/inventory.viewmodel.js" type="text/javascript"></script>

embedded in the @Section Scripts block, and then it works, but I still wonder if this is advisable.

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146

1 Answers1

0

There is nothing wrong with creating a bundle for each page, in fact, it is probably the best approach for your problem. Circumventing it would create more work, you would need to implement your own minification and compilation optimiser, just to support a subset of your JavaScript.

Michael Papworth
  • 453
  • 3
  • 11
  • I wouldn't *need* to do anything to the script. It runs just fine without minification/optimization. If my scripts are relatively small and likely to be in the cache, it seems like the impact would be minimal. But if there's no drawback to creating a separate bundle for each page, I might go that route. Would the startup overhead to processing a large number of bundles be negligible? – BlueMonkMN Nov 27 '13 at 15:38
  • Apologies, no you are quite right that you wouldn't _need_ to implement them; however, you _would_ lose that ability if you didn't. Having a large number of bundles won't cause you any problem. The start-up overhead is on per page basis and since you will only be including one additional bundle to render the view-models, this will be unnoticeable. – Michael Papworth Nov 27 '13 at 16:14
  • Does the minification happen every time the bundle is rendered instead of when the bundle is created? Or is it a lazy initialization type thing where it is minified the first time it is requested and then cached by the server for subsequent requests? – BlueMonkMN Nov 27 '13 at 16:40
  • The first time you hit a page that contains a reference to your bundle that is when it gets compiled, minified and optimised, then placed in a cache for all subsequent requests. I'll try and get a link the documentation I found that explains how the pipeline works. – Michael Papworth Nov 27 '13 at 16:53