Click here to Skip to main content
15,885,720 members
Articles / Web Development / HTML5
Tip/Trick

Convert a MVC4 Application to a "Cool Towel" Single Page Application (SPA)

Rate me:
Please Sign up or sign in to vote.
2.71/5 (4 votes)
26 Jan 2014CPOL2 min read 21.5K   1   6
Example project template for a "Cool Towel" SPA to be filled with a MVC4 application HTML5 and JavaScript code in order to convert
Screenshot running out-of-the-box Cool Towel SPA project template

Introduction 

This tip is about making website navigation just as fast as a desktop application and with the same look and feel, for example in an ASP.NET MVC4 internet application by converting it to a "Cool Towel" Single Page Application (SPA). This is done by putting the HTML5 code of each internet application page in the appropriate places of the SPA project template, also incorporating in this solution the corresponding JavaScript and controller code. A Cool Towel: You do not go to the SPA without one!

Background

A SPA, also known as single-page interface (SPI), is a web application or web site that fits on a single web page with the goal of providing a more fluid user experience akin to a desktop application. In a SPA, either all necessary code – HTML, JavaScript, and CSS – is retrieved with a single page load. The page does not reload at any point in the process, nor does control transfer to another page.

Using the Code

Open the SPA_Project_Template_01 solution in Visual Studio 2012 or later. Copy / paste the HTML5 code of your pages in the "XXXX HTML5 Here" placeholders in the Index.cshtml, add the JavaScript files, add the CSS files and controller files also. Be sure that all the JavaScript and CSS files are referenced in the Index.cshtml

Out-of-the-box the navigation buttons have the click event Knockout data binded (HTML5 code):

HTML
<div class="float-right">
    <nav>
        <ul id="menu1">
            <li>
                <input type="button" value="Home" data-bind="click: $root.divHome" /></li>
            <li>
                <input type="button" value="Manual" data-bind="click: $root.divManual" /></li>
            <li>
                <input type="button" value="Blog" data-bind="click: $root.divBlog" /></li>
            <li>
                <input type="button" value="Balance" data-bind="click: $root.divBalance" /></li>
            <li>
                <input type="button" value="Budget" data-bind="click: $root.divBudget" /></li>
            <li>
                <input type="button" value="About" data-bind="click: $root.divAbout" /></li>
            <li>
                <input type="button" value="Contact" data-bind="click: $root.divContact" /></li>
        </ul>
    </nav>
</div>

Out-of-the-box these click events trigger JQuery opening on collapsing of the placeholder <div>s, like (JavaScript code):

JavaScript
function appViewModel() {

    var self = this;

    self.divHome = function () {
        $("#divHome").show();
        $("#divManual").hide();
        $("#divBlog").hide();
        $("#divBalance").hide();
        $("#divBudget").hide();
        $("#divAbout").hide();
        $("#divContact").hide();
        $("#divTerms").hide();
        $("#divPrivacy").hide();
    }

Points of Interest

Navigation of the budget.satoconor.com demo website that took about 2 seconds according to the PageSpeed Chrome pluggin now is like instantaneous in a view microseconds. Moreover the bootstrap time of the entire SPA Index.cshtml, when application pool and website IIS are alive, has only doubled, from about 2 to 4 seconds, compared to loading the HOME page only of the MVC4 Internet Application.

History

  • 25th January, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer SATOCONOR
Netherlands Netherlands
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
QuestionMicrosoft: Change for the sake of change Pin
Johan G. van der Galien29-Jan-14 17:07
professionalJohan G. van der Galien29-Jan-14 17:07 
Not for the sake of improvement! Never mind the Sandersons, the Mamas and the Papas
QuestionMeant for simple websites, for the youth: The new generation developers Pin
Johan G. van der Galien29-Jan-14 5:43
professionalJohan G. van der Galien29-Jan-14 5:43 
Question[My vote of one] Total Fail Pin
alec.whittington27-Jan-14 4:58
alec.whittington27-Jan-14 4:58 
Question[My vote of 1] mmmm not sure Pin
Sacha Barber26-Jan-14 21:45
Sacha Barber26-Jan-14 21:45 
AnswerRe: [My vote of 1] mmmm not sure Pin
Johan G. van der Galien29-Jan-14 5:17
professionalJohan G. van der Galien29-Jan-14 5:17 
AnswerRe: [My vote of 1] mmmm not sure Pin
Johan G. van der Galien29-Jan-14 5:30
professionalJohan G. van der Galien29-Jan-14 5:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.