Today I will show you how to organize and load JavaScript on demand in ASP.Net MVC4 Views with KnockoutJS using RequireJS.
Here is my last article that explains the use of RequireJS with KnockoutJS in ASP.Net MVC4. You can have a look at them since it will help you to understand this article much better.
Organizing JavaScript in Views
What will happen if you fail to organize the JavaScript code in an ASP.Net MVC view? Of course, you will end up with the loading order of JavaScript and if you referenced all the scripts in the same page then it will lead you to have a collision on the JavaScript function's name. So it is very important to organize the JavaScript in MVC views.
How are we going to organize the JavaScript on views? Well, as we will use RequireJS, it will be much easier to organize them. We have a two-step process to organize the JavaScript in ASP.Net MVC4.
- Use RequireJS or StealJS to load JavaScript on demand asynchronously.
- Have an "Initializer" section in each page that will load only the required JavaScript models and viewmodels.
- Render the "Initializer" section from the master page, probably _Layout.cshtml in ASP.Net MVC4 that is defined in each page, only when the specific page is loaded into the master page.
Let us explain this with an example project.
Setup the Project
Since we will use the same example project that we created for my last article, please follow the steps provided there.
If you are done with the project setup then please ensure that the necessary JavaScript files are added. Cross-check with the figure shown below.
In this above example, we have two new files added.
The config.js
Here the config.js holds the configuration settings required for RequireJS.
var require = {
baseUrl: "/Js",
paths: {
ko: "/Scripts/knockout-2.3.0",
jquery: "/Scripts/jquery-1.8.2.min"
}
};
It is always good to have a separate config file for RequireJS. The syntax for the separate config is that the config object should be assigned to the variable that should be named as "require" as shown in the preceding example.
The home-content.js
The home-content.js is nothing but a JavaScript file for the Content.cshtml view's knockout viewmodel.
define('home-content', ['ko'], function (ko) {
var contentViewModel = function (options) {
var self = this;
self.welcomeMessage = ko.observable('This is ' + options.title + ' page');
self.onmousein = function (data, event) {
$(event.target).animate({ fontSize: "24px" }, 1500);
},
self.onmouseout = function (data, event) {
$(event.target).animate({ fontSize: "12px" }, 1500);
};
};
return contentViewModel;
});
This file has the knockout viewmodel defined by RequireJS for the Content view. RequireJS defines the knockout viewmodel with the moduleId 'home-content'. When the dependency module (for KonckoutJS) is loaded, the given callback function will be loaded with the ko object as a parameter.
Note: Each define()'s callback function should return the viewmodel created else you won't be getting the viewmodel object when you access the defined viewmodel from require().
The Content.cshtml
In this view, as usual we have simple welcome text. But this time, we have a link for another view.
@{
ViewBag.Title = "Content";
}
<section id="content-page">


Join the conversation! Your thoughts help the community grow.