Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - is there a nice way to bundle controls with their respective javascript?

I'm pretty new to MVC and I can't decide on the best way to store cshtml files and their respective javascript code. Some JS code in my project needs to run globally, but most of it is entirely tied to specic views or partial views.

If I put the javascript in the views, I get a mess of inline uncacheable javascript, if I put it in one central file, I lose modularity.

I heard that in MVC4 there are going to be minification features, is there something I can do with MVC3 that will allow me to choose in the Views which javascripts to include and then group them and minify them automatically? (maybe even in groups?)

like image 292
Madd0g Avatar asked Mar 06 '12 20:03

Madd0g


People also ask

How can create JavaScript bundle in MVC?

How to create and use a ScriptBundle in ASP.NET MVC? JavaScript bundles can be created only in ~/App_Start/BundleConfig. cs page. To create a JavaScript bundle we use ScriptBundle class by passing virtual path as parameter in the constructor.

How do we implement bundling in MVC?

To enable bundling and minification, set the debug value to "false". You can override the Web. config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.

What bundling allows in MVC?

Bundling and minification techniques were introduced in MVC 4 to improve request load time. Bundling allows us to load the bunch of static files from the server in a single HTTP request. In the above figure, the browser sends two separate requests to load two different JavaScript file MyJavaScriptFile-1.

What is the benefit of bundling .js scripts into one file?

Bundling reduces the number of individual HTTP requests to server by combining multiple CSS files and Javascript files into single CSS file and javascript file. Minification reduces the file download size of CSS and javascript files by removing whitespace, comments and other unnecessary characters.


1 Answers

Cassette it's essentially the same thing as the upcoming MVC4 bundles.

In your view page, you can reference scripts and stylesheets using Cassette's Bundles helper class.

@{
    Bundles.Reference("Scripts/jquery.js");
    Bundles.Reference("Scripts/page.js");
    Bundles.Reference("Styles/page.css");
}
<!DOCTYPE html>
    <html>
...

In addition, Cassette has native support for Less and CoffeScript. It has also support for HTML Templates, if you are interested in client side MVC frameworks like Knockout.js or Backbone.js.

Still you have to choose how to group your content. As the official documentation is suggesting, probably the best choice is to treat bundles as units of deployment.

Keep in mind that a bundle is a unit of deployment. If any asset in a bundle changes, then the entire bundle has to be downloaded again by web browsers. So perhaps group shared code into a bundle and put page scripts into their own bundles.
like image 170
Paolo Moretti Avatar answered Nov 02 '22 23:11

Paolo Moretti